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 = 25if temperature > 20 { print("Warm")}print("done")WarmdoneThe 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 = 11if age >= 13 { print("You can join the club.")}else { print("Come back in a year or two!")}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.
Choosing among several paths
Section titled “Choosing among several paths”else if adds another condition to try when the ones before it were false:
const temperature = 12if temperature < 0 { print("Freezing")}else if temperature < 20 { print("Cool")}else { print("Warm")}CoolThe 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.
Blocks inside blocks
Section titled “Blocks inside blocks”A block can hold any code, including another if:
const age = 15const member = trueif age >= 13 { if member { print("Welcome back!") }}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)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.
One action, one line
Section titled “One action, one line”When a condition guards a single action, the if can go after it, on one line:
const score = 150print("Bonus!") if score > 100print("Tiny score") if score < 10Bonus!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.
Choosing a value
Section titled “Choosing a value”Sometimes the decision is between two values rather than two actions. if ... then ... else gives
one value or the other:
const score = 12const label = if score >= 10 then "winner" else "playing"print(label)winnerBoth answers are needed, since the expression must have a value either way:
const n = 3const size = if n > 2 then "big"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.
Braces are required
Section titled “Braces are required”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.
What you’ve learned
Section titled “What you’ve learned”if condition { ... }runs a block only when the condition istrue.elseruns when it isfalse, andelse iftries another condition; the first true one wins.- A name declared in a block exists only inside it.
action if conditionguards one action on one line.if condition then a else bchooses between two values, and needs both.
Next, case: choosing among many possibilities for one value.