Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Conditionals

if / elif / else / endif blocks execute based on a comparison between two values.

if age >= 18
    println 'Adult'
else
    println 'Minor'
endif
  • Every if must be closed with endif.
  • else and elif are optional.
  • The condition uses one of the comparison operators: ==, !=, >, <, >=, <=.

Chaining Conditions

Use elif (else-if) to chain multiple conditions without nesting:

if mode == 1
    println 'Mode one'
elif mode == 2
    println 'Mode two'
else
    println 'Unknown mode'
endif

You can chain as many elif blocks as needed:

if number % 15 == 0
    println 'Divisible by 15'
elif number % 3 == 0
    println 'Divisible by 3'
elif number % 5 == 0
    println 'Divisible by 5'
else
    println 'Not divisible by 3 or 5'
endif

Inline Expressions in Conditions

Conditions can include arithmetic and other expressions directly:

number = 15

if number % 15 == 0
    println 'Divisible by 15'
elif number % 3 == 0
    println 'Divisible by 3'
endif

See also Loops, FizzBuzz, and Temperature Converter for examples of conditionals in action.