Skip to main content

Version: 19.11 (EOL)

Control Statements

Groovy control statements are modelled on C-style statements. You can do a lot with very little.

Branching Logic

Groovy supports if/else statements, switch statements and the ternary operator. Generally, you should be able to get by with using if/else statements.

If/Else Statements

Here is an example if/else statement.

def seats = 2

if (seats == 0) {
println 'There are no seats'

} else if (seats == 1) {
println 'There is one seat'

} else if (seats == 2) {
println 'There are two seats'

} else {
println 'There are more than two seats, if you think positively'

}

The output of this script is shown below.

There are two seats

The Ternary Operator

The ternary operator is a compact statement that combines an if/else statement with a variable assignment.

def seats = 1
def msg = (seats == 2) ? "There are two seats left" : "Sorry we don't have 2 seats left"
println msg

The output of this script is shown below.

Sorry we don't have 2 seats left

Looping

For Statement

Using the for statement, you can loop over a range of numbers, lists and maps.

Number Iteration

A for statement requires you to specify a starting index, the iteration test, and an increment operation.

def items = [ "banana", "apple", "orange", "mango", "grapes", "kiwi fruit" ] 

// Start at index 0, only loop while the index is less than the length of the list,
// and increment the index by one for each iteration
for (int i = 0; i < items.size(); i++) {
println i + ". " + items[i]
}

The output of this script is shown below:

0. banana
1. apple
2. orange
3. mango
4. grapes
5. kiwi fruit

List Iteration

When looping through a list, you can use the "for each item in items" syntax.

def items = [ "banana", "apple", "orange", "mango", "grapes", "kiwi fruit" ] 

// For each item in the list of items
for (item in items) {
println item
}

The output of this script is shown below.

banana
apple
orange
mango
grapes
kiwi fruit

Map Iteration

Maps are special in that you can iterate through them a number of ways.

def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]

// For each map entry
for (entry in car) {
println entry.key + ": " + entry.value
}
def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]

// For each map entry key (name)
for (key in car.keySet()) {
println key + ": " + car[key]
}
def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]

// For each value in the map
for (value in car.values()) {
println value
}

To find something in a map, you can generally look up the value using the key.

Returning Values

In a Groovy script, you can return an object to the calling code using the return statement.

def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]

if (car["doors"] == 2) {
return "We don't insure sports cars"
}

println 'Continue processing'

// Continue processing
...

Catching Errors

If you need to handle errors and continue processing, you can use the try / catch statement.

def textValue = "z123"

def intValue
try {
intValue = Integer.parseInt(textValue)

} catch (Exception e) {
println e
intValue = -1
}

println "Integer value: " + intValue