if / else
Try it yourself →Branching based on conditions.
Syntax
if (condition) { ... } else { ... }Description
The condition is written inside parentheses; if true, the first block runs, otherwise else runs. Intermediate conditions use else if.
Example
def age = 20
if (age >= 18) {
println "Adult"
} else {
println "Child"
}Expected output
Adult
When to use?
Decision making, input control.