5. Making decisions: if
Until chapter 4 every program had a single path: straight down, every line executed. Real programs need to choose — put on a coat when it is cold, upgrade when the score is high, apply a discount for members. This chapter is about how Sprig chooses.
In this chapter you will learn:
- to let a program pick one of several paths with
if,elifandelse; - that a block is indentation, made of spaces and never tabs;
- why the compiler rejects
else ifwith a message of its own; - to use
ifas an expression that produces a value.
5.1 If this, do that
The simplest choice: when a condition holds, run a block of code.
let temperature = 30
if temperature > 25:
print("wear a t-shirt")
print("go outside")wear a t-shirt
go outsideLine by line:
let temperature = 30binds 30 to the nametemperature(chapter 3).if temperature > 25:is "if".temperature > 25is aBoolexpression, called the condition; the:at the end says "the indented block below belongs to me".print("wear a t-shirt")is indented by four spaces, so it is inside theifblock. The condition istrue(30 is indeed greater than 25), so the line runs.print("go outside")is not indented, so it is outside the block and always runs. Change 30 to 10 and the first line stops printing while the second still does.
Indentation is structure. Braces {} and trailing semicolons do not exist here; don't write them.
5.2 The other path: else
When the condition fails and you want a different path, add else:
let age = 15
if age >= 18:
print("welcome")
else:
print("not yet")not yetage is 15, so age >= 18 is false; the if block is skipped and the else block runs, printing not yet. else carries no condition of its own, and it is written in the same column as if. Exactly one of the two blocks ever runs.
5.3 More than two paths: elif
For three or more paths, continue with elif (short for else-if):
var celsius = -5
if celsius < 0:
print("freezing")
elif celsius < 20:
print("cool")
else:
print("warm")
celsius = 12
if celsius < 0:
print("freezing")
elif celsius < 20:
print("cool")
else:
print("warm")
celsius = 30
if celsius < 0:
print("freezing")
elif celsius < 20:
print("cool")
else:
print("warm")freezing
cool
warmThe program runs the same decision three times, changing celsius first each time. The variable is declared with var on line 1 because its value changes.
celsius = -5:-5 < 0is true, so it printsfreezingand every later branch is skipped.celsius = 12: the first condition is false;12 < 20is true, so it printscool.celsius = 30: the first two conditions are false, so it falls through toelseand printswarm.
The conditions are checked from top to bottom and the chain is left as soon as one matches, so at most one branch runs. else is optional, but it always goes last. The three copies differ only in their data — chapter 6 uses loops to get rid of that repetition.
5.4 The indentation rule: spaces, not tabs
Blocks are indentation, and the choice is strict: spaces only. Press the Tab key and the compiler rejects the file, even if the tab looks exactly as wide as the spaces around it. This program indented line 3 with a Tab:
let n = 3
if n > 1:
print("big")SPR-LEX-TAB [LEX] main.spr:3:1: Tabs are not allowed for indentation or inline whitespace
hint: Sprig code blocks use spaces only; replace the tab with spaces.
SPR-SYNTAX-ERROR [SYNTAX] main.spr:3:2: Expected an indented block after 'if ...:'How to read it:
- The first line is the important one.
main.spr:3:1is the position: filemain.spr, line 3, column 1.SPR-LEX-TABis the diagnostic code — a tab error caught in the lexer;sprig explain SPR-LEX-TABprints the full explanation. - The
hint:line gives the fix directly: replace the tab with spaces. Setting your editor to "insert spaces for tabs" saves a lot of trouble. - The second line is a consequence of the first: once the tab is rejected, the parser never sees an indented block under the
if. Fix the first problem and this one usually disappears.
Four spaces per level is the convention, and the examples here use it. Every line in the same block must line up; the compiler also accepts different widths in different blocks (one block with 2 spaces and another with 4, say), but keeping the book's width everywhere reads better.
5.5 Deliberate mistake: else if
People coming from Java, JavaScript or C almost always write else if for the second condition. In Sprig it is spelled elif:
let n = 3
if n > 5:
print("big")
else if n > 1:
print("mid")SPR-SYNTAX-ERROR [SYNTAX] main.spr:4:6: Sprig spells else-if as 'elif'
hint: Write 'elif condition:' in place of 'else if condition:'.4:6 points at the misplaced else. The compiler recognises this habit from other languages and, instead of a generic syntax error, tells you the right spelling — replace else if with elif and you are done.
5.6 if as a value
Sometimes you do not want "which block runs" but "which value is chosen", for example a size picked from a count. if can also stand on the right-hand side of = and hand the chosen value straight to a variable:
let count = 3
let size = if count > 9:
"many"
elif count > 0:
"some"
else:
"none"
print(size)someHere if is an expression: the whole structure produces a value, and size receives "some". The rules are few:
- Each branch is one expression on its own indented line, not a block of statements; the branches here are
"many","some"and"none". elifandelseline up with theif;elseis required, because an expression must always have a value.- Every branch must produce the same type — here they are all
String. - It can only appear where a value belongs: after
=, afterreturn(chapter 7), afterthrow(chapter 14), and similar places. Anifat the start of a statement is still the statement, whoseelseis optional.
Many languages use the condition ? a : b ternary operator; Sprig has none. The if expression is what it has instead.
5.7 Deliberate mistake: an if expression inside a call
Since an if expression is a value, it is tempting to put one into print(...):
let n = 3
print(if n > 0:
"yes"
else:
"no")SPR-SYNTAX-ERROR [SYNTAX] main.spr:2:7: An if expression cannot be written inside parentheses, brackets or braces
hint: Line breaks are ignored there, so the branches cannot go on their own lines. Bind the if expression, or the lambda that holds it, to a let first, then use the name.The hint explains why: line breaks inside parentheses, brackets or braces are ignored, and each branch of an if expression must have a line of its own. Bind the result to a name first, then pass the name:
let n = 3
let label = if n > 0:
"yes"
else:
"no"
print(label)yesSummary
if condition:opens one path; the condition must be aBool.elifchecks the next path,elsecatches the rest; at most one branch runs.- Blocks are indentation (four spaces), spaces only, never tabs.
else ifis spelledelif.- An if expression on the right of
=produces a value: one line per branch, matching types, andelseis required. - A standalone
ifis the statement, whoseelseis optional; an if statement cannot sit inside a call as an argument.
Exercises
Exercise 1: positive, negative or zero
n is -7. Print the right one of positive, negative or zero.
Hint: one if / elif / else chain testing n > 0 and n < 0.
Answer
let n = -7
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")negativeExercise 2: letter grades
score is 85. Print the grade: A for 90 and above, B for 80 and above, C for 60 and above, otherwise F.
Hint: check from the top down—ask score >= 90 first, then score >= 80.
Answer
let score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")
else:
print("F")BExercise 3: backpack size
bottles is 5: print large for more than 9, medium for more than 3, and small otherwise. This time use an if expression: assign it to size in one go, then print(size).
Hint: each branch holds a string, and the else holds "small".
Answer
let bottles = 5
let size = if bottles > 9:
"large"
elif bottles > 3:
"medium"
else:
"small"
print(size)mediumExercise 4: member discount
is_member is true and the total is 120. The rules: print no discount when not a member; when a member, print 20% off if the total is at least 100 and 10% off otherwise.
Hint: an outer if is_member: covers membership, and an inner total >= 100 covers the amount — a block can contain another block, indented four spaces further.
Answer
let is_member = true
let total = 120
if is_member:
if total >= 100:
print("20% off")
else:
print("10% off")
else:
print("no discount")20% offComing from another language?
- Python: colons, indentation and
elifare the same; but the condition must really be aBool, soif 0:orif "":will not compile. Python'sa if cond else bbecomes the multi-line if expression here. - Java / JavaScript / C: there is no
else if— writeelif; there is no?:ternary; braces and semicolons are left out and blocks are indentation. - Deep nesting is hard to read. The early
returnof chapter 7 flattens nestedifs.
Next chapter: Chapter 6: Repeating: loops.
