If Else in Groovy On this page Branching with if
and else
in Groovy is straightforward.
// Here's a basic example.
if ( 7 % 2 == 0 ) {
println "7 is even"
} else {
println "7 is odd"
}
// You can have an `if` statement without an else.
if ( 8 % 4 == 0 ) {
println "8 is divisible by 4"
}
// Logical operators like `&&` and `||` are often
// useful in conditions.
if ( 8 % 2 == 0 || 7 % 2 == 0 ) {
println "either 8 or 7 are even"
}
// A statement can precede conditionals; any variables
// declared in this statement are available in the current
// and all subsequent branches.
def num = 9
if ( num < 0 ) {
println "$num is negative"
} else if ( num < 10 ) {
println "$num has 1 digit"
} else {
println "$num has multiple digits"
}
To run this Groovy script, save it to a file (e.g., if_else.groovy
) and execute it using the groovy
command:
$ groovy if_else.groovy
7 is odd
8 is divisible by 4
either 8 or 7 are even
9 has 1 digit
Note that in Groovy, parentheses around conditions are optional, but they’re often used for clarity. Braces are required for multi-line blocks but can be omitted for single-line statements.
Groovy also supports the ternary operator (?:
) and the Elvis operator (?:
) for concise conditional expressions:
def result = ( 7 % 2 == 0 ) ? "7 is even" : "7 is odd"
println result
def nullableValue = null
def nonNullValue = nullableValue ?: "default value"
println nonNullValue
These operators can be useful for simple conditions, but for more complex logic, full if
/else
statements are recommended.
Branching with `if` and `else` in Groovy is straightforward.
// Here's a basic example.
if ( 7 % 2 == 0 ) {
println "7 is even"
} else {
println "7 is odd"
}
// You can have an `if` statement without an else.
if ( 8 % 4 == 0 ) {
println "8 is divisible by 4"
}
// Logical operators like `&&` and `||` are often
// useful in conditions.
if ( 8 % 2 == 0 || 7 % 2 == 0 ) {
println "either 8 or 7 are even"
}
// A statement can precede conditionals; any variables
// declared in this statement are available in the current
// and all subsequent branches.
def num = 9
if ( num < 0 ) {
println "$num is negative"
} else if ( num < 10 ) {
println "$num has 1 digit"
} else {
println "$num has multiple digits"
}
To run this Groovy script, save it to a file (e.g., `if_else.groovy` ) and execute it using the `groovy` command:
$ groovy if_else.groovy
7 is odd
8 is divisible by 4
either 8 or 7 are even
9 has 1 digit
Note that in Groovy, parentheses around conditions are optional, but they're often used for clarity. Braces are required for multi-line blocks but can be omitted for single-line statements.
Groovy also supports the ternary operator (`?:`) and the Elvis operator (`?:`) for concise conditional expressions:
def result = ( 7 % 2 == 0 ) ? "7 is even" : "7 is odd"
println result
def nullableValue = null
def nonNullValue = nullableValue ?: "default value"
println nonNullValue
These operators can be useful for simple conditions, but for more complex logic, full `if` /`else` statements are recommended.