Skip to content
On this page

Statements: If

The if statement is used to conditionally execute a block of code if a condition is true.

syntax
IfStatement :
  if Expression BlockStatement ElseIf* Else?
 
ElseIf :
  elseif Expression BlockStatement
 
Else :
  else ( BlockStatement | IfStatement )


if $condition {
    // ...
}

An else clause can be used to execute a block of code if the condition is false.

if $condition {
    // ...
} else {
    // ...
}

An else if clause can be used to chain multiple conditions.

if $condition1 {
    // ...
} else if $condition2 {
    // ...
} else {
    // ...
}

TIP

You can use the elseif keyword instead of else if.

if $condition1 {
    // ...
} elseif $condition2 {
    // ...
} else {
    // ...
}

WARNING

Unlike PHP, Ara does not support one-line if statements.

function example(bool $condition): void {
    if ($condition) foo();
}

The code above will result in the following error:

error[P0042]: unexpected identifier, expected `{`
  ┌─ examples/if-one-line.ara:2:21

2 │     if ($condition) foo();
  │                     ^^^ unexpected identifier, expected `{`

Licensed under either of the MIT License or the Apache License (Version 2.0), at your option.