What an if statement does
A program that always does the same thing is limited. An if statement lets code run only when a condition holds, the same way you decide during a day: if it's rainy, bring an umbrella; if you're hungry, have breakfast.
The shape is always the same: the keyword if, a condition in parentheses, and a block in curly braces.
if (condition) {
// instructions
}
Scala runs the block only when the condition is true. When it's false, Scala skips the block and continues with whatever follows.
Boolean conditions
The condition must be a Boolean, a value that is either true or false. It can be a Boolean variable, a comparison such as age >= 18, or any other expression of that type.
val isRainy = true
if (isRainy) {
println("I bring an umbrella")
}
val amHungry = false
if (amHungry) {
println("I have breakfast")
}
println("done")
This prints:
I bring an umbrella
done
The first block runs because isRainy is true. The second is skipped because amHungry is false, and the program carries on to the last line.
Only a Boolean will do
Scala is strict about the condition's type. Unlike some languages, it doesn't treat numbers or strings as "truthy" or "falsy". Writing if (n) where n is an Int fails to compile with a type mismatch: found Int, required Boolean.
A single = is a related trap. It assigns, while == compares. Writing if (n = 5) doesn't compile either, because an assignment has type Unit and the compiler requires a Boolean. In Scala this slip is caught before the program runs.
Parentheses and braces
The parentheses around the condition are required. The braces are optional when the block holds one statement, so if (isRainy) println("I bring an umbrella") works. Braces are still a good habit, because they make it obvious where the block ends when you add a second line later.
The exercise
The starter code declares one value:
val passed: Boolean = true
The task has three steps.
- Write the
if.passedis already aBoolean, so the condition is justpassed. Comparing it with== trueadds nothing. - Print inside the block. Put
println("Passed the test")between the braces, so it runs only when the condition is true. - Add an
else. Anelsehas no condition of its own. It covers every case theifdidn't. Write} else {on one line, print"Failed"inside, and close the block.
The finished code:
val passed: Boolean = true
if (passed) {
println("Passed the test")
} else {
println("Failed")
}
With passed set to true it prints Passed the test. Change the value to false and it prints Failed. Exactly one of the two blocks runs, never both and never neither.
Common mistakes
- Forgetting the parentheses. The condition always goes in
( ). - Using
=instead of==. One assigns, the other compares. - Misspelling the message. The text in the quotes is the output, so
"Passed the test"and"Passed the Test"are different. - Writing a condition after
else.elsenever takes one. A second check needselse if.
Key takeaways
if (condition) { ... }runs the block only when the condition istrue.- The condition must be a
Boolean; Scala has no truthy or falsy values. - An
elseblock runs when the condition isfalse. - When the condition is already a
Booleanvalue, use it directly.
