To execute a block of code several number of times you would need Control structures. Groovy supports the usual
- Conditional statements:
if-else, "nested" if then else if, switch, try-catch-finally
- Looping statements:
for, for in, while loop
- Branching statements:
break, continue, return
On this page:
Table of Contents
Conditional statements
Conditional statements execute a set of statements only if the condition is true
...
Code Block | ||||
---|---|---|---|---|
| ||||
//Simple if if(x==2){ false } //Simple if else if(x == 2){ x = x + 2 } else{ x = x - 2 } //Nested if if (x) { x = x + 1 } else if (y) { y = y + 1 } else { 0 } //Assign and test in nested expression if ((x = 3)) { return true } //Optional return statement def sysName = "Windows" if (sysName.contains("Windows")) "We're on Windows." else "Oh, well we are on Mac" |
Switch statement
The switch statement executes one statement from multiple conditions. It is like if-else-if
ladder statement. The switch statement in Groovy can handle any kind of switch value and different kinds of matching can be performed.
...
Code Block | ||||
---|---|---|---|---|
| ||||
def myMethod() { throw new IllegalArgumentException() } def log = [] try { myMethod() } catch (Exception e) { log << e.toString() } finally { log << "finally" } log.size() == 2 |
Looping statements
Looping repeats the execution of a block of code multiple times. The loops available in Groovy are covered below.
while loop
In a while loop the boolean test is evaluated, and if it's true, the body of the loop is then executed.
Code Block | ||||
---|---|---|---|---|
| ||||
def list = [1, 2, 3] while (list){ list.remove(0) } list |
Note: There is no do-while loop in Groovy
for/for-in loop
for / for-in loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop. In Groovy the for loop is much simpler and works with any kind of array, collection, Map, etc.
Code Block | ||||
---|---|---|---|---|
| ||||
// Normal for loop for (i = 0; i < 5; i++) { i; } // iterate over a range def x = 0 for ( i in 0..9 ) { x += i } x == 45 // iterate over an array def array = (0..4).toArray() x = 0 for ( i in array ) { x += i } x == 10 //iterate over a list def x = 0 def list = [10,12,13,14]; for(j in list){ x += j; } x == 49 //iterate over a map def x = 0 def customers = [ 0 : "David", 1 : "Elle", 2 : "Peaches" ]; for(cust in customers){ x += cust.key } x == 3 //iterate over values of a Map def x = "" def customers = [ 0 : "David", 1 : "Elle", 2 : "Peaches" ]; for(cust in customers){ x += cust.value x += "," } x == "David,Elle,Peaches" // iterate over the characters in a string def text = "abc" def list = [] for (c in text) { list.add(c) } list == ["a", "b", "c"] |
...
The continue statement complements the break statement. Its use is restricted to while and for loops, explained above.
Return statement
The last line of a method in Groovy is automatically the return statement. For this reason, an explicit return statement can be left out.