
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
Groovy - For Statement
The for statement is used to iterate through a set of values. The for statement is generally used in the following way.
for(variable declaration;expression;Increment) { statement #1 statement #2 }
The classic for statement consists of the following parts −
Variable declaration â This step is executed only once for the entire loop and used to declare any variables which will be used within the loop.
Expression â This will consists of an expression which will be evaluated for each iteration of the loop.
The increment section will contain the logic needed increment the variable declared in the for statement.
The following diagram shows the diagrammatic explanation of this loop.

Example - Basic Usage of for Statement
Following is an example of the classic for statement −
Example.groovy
class Example { static void main(String[] args) { for(int i = 0;i<5;i++) { println(i); } } }
Explanation
In the above example, we are in our for loop doing three things −
Declaring a variable i and Initializing the value of i to 0
Putting a conditional expression that the for loop should execute till the value of i is less than 5.
Increment the value of i by 1 for each iteration.
Output
The output of the above code would be −
0 1 2 3 4
Example - Printing List Elements Using for Loop
Following is an example of the iterating list elements using for statement −
Example.groovy
class Example { static void main(String[] args) { def numbers = [10, 20, 30, 40, 50]; for(int index = 0; index < numbers.size(); index++) { println("value of item : " + numbers[index] ); } } }
Output
The output of the above code would be −
value of item : 10 value of item : 20 value of item : 30 value of item : 40 value of item : 50
Explanation
In this example, we're showing the use of a for loop to print contents of a list. Here we're creating an list of integers as numbers and initialized it some values. We've created a variable named index to represent index of the list within for loop, check it against size of the list and incremented it by 1. Within for loop body, we're printing element of the list using index notation. Once index becomes same as list size, for loop exits and program quits.
Example - Nested for Loops
Following is an example of using nested for loop statement useful to create/iterate matrices−
Example.groovy
class Example { static void main(String[] args) { for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { print(i + "," + j + " "); } println(); } } }
Output
The output of the above code would be −
1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3