Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

if - else statement

Groovy supports the usual if - else syntax from Java

Code Block
def x = false
def y = false

if ( !x ) {
    x = true
}

assert x == true

if ( x ) {
    x = false
} else {
    y = true
}

assert x == y

if - else statement

Groovy also supports the ternary operator:

Code Block
def y = 5
def x = (y > 1) ? "worked" : "failed"
assert x == "worked"

See also: the elvis operator

switch statement

The switch statement in Groovy is backwards compatible with Java code; so you can fall through cases sharing the same code for multiple matches.

...