Flow control

From Reia

Jump to: navigation, search

Contents

Branching

if and unless

The simplest branching construct available is the if statement, which takes the specified branch if the condition holds true:

if condition
  ...

The else keyword specifies an action to be taken if the condition doesn't match:

if condition
  ...
else
  ...

Multiple if statements can be chained with the elseif keyword:

if condition1
  ...
elseif condition2
  ...

The unless keyword functions like the if keyword, except expression matching is inverted and the branch is taken if the condition does not hold.

unless condition
  ...

Branch statements can be used inline by appending them to the end of statements:

do_this() if condition
dont_do_this() unless condition

The case statement

case allows you to match several possible conditions against a variable:

case condition
  42:
    ...
  ~something:
    ...
  else:
    ...

The case statement uses pattern matching for each value of the when clause. Patterns must be indented one level beyond the case declaration, and end with a ':' token.

Loops

Sorry, loops are presently unimplemented

The while loop executes so long as the specified condition holds true:

while condition
  ...

The until loop executes so long as the condition is false:

until condition
  ...

The while and until statements can be placed at the end of the loop as well by using the do keyword:

do
  ...
while condition

For loops can iterate lists:

for x in mylist
  ...

This works well when using ranges to generate the list:

for x in 1..10
  ...
Personal tools