Skip to content

If and Else

So far every program has run each line, top to bottom. if lets a program decide: it runs a block of code only when a condition is true.

const temperature = 25
if temperature > 20 {
print("Warm")
}
print("done")
Output
Warm
done

The condition, temperature > 20, comes after if, and the code to run goes between braces, { and }. The code inside is indented by four spaces so a reader can see it belongs to the if. Change the temperature to 15 and only done is printed.

The condition must be a Bool: a comparison, a true or false value, or a question such as name.empty?(). Booleans and Comparison covers them.

else gives the code to run when the condition is false:

const age = 11
if age >= 13 {
print("You can join the club.")
}
else {
print("Come back in a year or two!")
}
Output
Come back in a year or two!

Exactly one of the two blocks runs, never both. else starts its own line, after the closing brace.

else if adds another condition to try when the ones before it were false:

const temperature = 12
if temperature < 0 {
print("Freezing")
}
else if temperature < 20 {
print("Cool")
}
else {
print("Warm")
}
Output
Cool

The conditions are tried in order, and the first one that is true wins; the rest are skipped. So temperature < 20 only needs to mean “cool”, because freezing temperatures were already taken care of. The final else catches everything left over, and can be left off if nothing should happen then.

When there are many cases for one value, case is often clearer.

A block can hold any code, including another if:

const age = 15
const member = true
if age >= 13 {
if member {
print("Welcome back!")
}
}
Output
Welcome back!

Often the two conditions can be joined instead, if age >= 13 and member, which is easier to read.

A name declared inside a block belongs to that block and disappears when it ends:

if true {
const message = "inside"
}
print(message)
Output
decisions.em:4:7: [E1001] `message` is not defined
print(message)
^^^^^^^
Check the spelling, or declare it before this line.

To use a value after the block, declare the name before it and assign it inside, as Variables and Constants showed.

When a condition guards a single action, the if can go after it, on one line:

const score = 150
print("Bonus!") if score > 100
print("Tiny score") if score < 10
Output
Bonus!

This form suits a short action and has no else. It works with a call, an assignment, return, break, or continue, but not a declaration.

Sometimes the decision is between two values rather than two actions. if ... then ... else gives one value or the other:

const score = 12
const label = if score >= 10 then "winner" else "playing"
print(label)
Output
winner

Both answers are needed, since the expression must have a value either way:

const n = 3
const size = if n > 2 then "big"
Output
decisions.em:2:33: an `if` used as a value needs an `else`
const size = if n > 2 then "big"
^
Give a value for both outcomes: `if condition then value else other_value`.

And both answers must be the same kind of value: if n > 2 then "big" else 7 is an error, because size would be text or a number depending on n.

A block always has braces, even when it holds a single line. if n > 0 print("positive") is an error. This keeps every block looking the same, and there is no way to be misled about which lines an if controls.

The opening brace can go at the end of the if line, as on this page, or on the line after it; emerald format keeps a whole project to one style, which a project can choose in its emerald.toml.

  • if condition { ... } runs a block only when the condition is true.
  • else runs when it is false, and else if tries another condition; the first true one wins.
  • A name declared in a block exists only inside it.
  • action if condition guards one action on one line.
  • if condition then a else b chooses between two values, and needs both.

Next, case: choosing among many possibilities for one value.