Control structures

In this section, we will discuss the basic control structures, namely the if…else statement, the switch statement, the for loop, and the while loop.

The if-else condition

The if…else condition in Groovy is similar to Java with one exception, how Groovy evaluates the logical if condition. In the following example, the if condition is evaluated true for both Boolean and int values. In Groovy, non-zero integers, non-null values, nonempty strings, initialized collections, and a valid matcher are evaluated as Boolean true values. This is known as Groovy Truths. Let's take a look at the following code:

def condition1 = true
int condition2 = 0
if(condition1){
  println("Condition 1 satisfied")
  if(condition2){
    println("Condition 2 satisfied")
  }else{
    println("Condition 2 failed")
  }
}else{
  println("Condition 1 failed")
}

Groovy also supports ternary operators (x? y: z), such as Java, which can be used to write the standard if-else logic:

(condition2> 0 )? println("Positive") : println("Negative")

Groovy also provides one additional operator known as the Elvis operator. It can be used as a shorter version of the ternary operator in the scenario, where the user wants to validate a variable against the null value. Consider the following example:

def inputName
String username = inputName?:"guest"

If inputName is not null username would be inputName else default value "guest" would be assigned to username

The switch statement

Groovy supports Class, Object, Range, Collection, Pattern, and Closure as classifiers in the switch statement. Anything that implements the isCase method can be used as a classifier in the switch statement. The following example shows a case defined for various classifiers. Just try different values of input and observe the output of the switch statement:

def checkInput(def input){
switch(input){    
  case [3, 4, 5]   :   println("Array Matched"); break;
  case 10..15      :   println("Range Matched"); break;
  case Integer     :   println("Integer Matched"); break;
  case ~/w+/      :   println("Pattern Matched"); break;
  case String      :   println("String Matched"); break;
  default          :   println("Nothing Matched"); break;
}
}
checkInput(3)  // will print Array Matched
checkInput(1)  // will print Integer Matched
checkInput(10)  // will print Range Matched
checkInput("abcd abcd") // will print String Matched
checkInput("abcd")  // will print Pattern Matched

Loops

Groovy supports both for (initialize; condition; increment) and for-each type looping. The for-each style is expressed as for(variable in Iterable) { body}. As loop works on an iterable object collection, it can be easily applied to array, range, collections, and so on. Let's take a look at the following code:

// Traditional for loop
for(int i = 0; i< 3; i++) {/* do something */ }
// Loop over a Range
for(i in 1..5) println(i)
// Array iteration
def arr = ["Apple", "Banana", "Mango"]
for(i in arr) println(i)
// for applied on Set
for(i in ([10,10,11,11,12,12] as Set)) println(i)

The while loop is similar to the Java while loop, though Groovy doesn't support the do-while style of looping. Let's demonstrate the while loop:

int count = 0
while(count < 5) {
  println count++
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.225