What comparison operators do
An if statement needs a Boolean condition, and most of the time that condition comes from comparing two values. A comparison operator looks at the values on both sides and gives back true or false.
| Operator | Meaning | Examples |
|---|---|---|
== | equal to | x == 5, x == y |
!= | not equal to | x != 3, x != y |
> | greater than | x > 5, x > y |
< | less than | x < 8, x < y |
>= | greater than or equal to | x >= 4, x >= y |
<= | less than or equal to | x <= 3, x <= y |
Each of these is an expression with a value:
10 == 10 // true
3 < 2 // false
15 >= 15 // true
17 != 12 // true
Comparisons inside if statements
Usually you compare variables:
var a = 10
var b = 15
if (a == b) {
println("a is equal to b")
}
if (a < b) {
println("a is less than b")
}
The first condition is 10 == 15, which is false, so its block is skipped. The second is 10 < 15, which is true. The program prints a is less than b.
Scala details
==compares values. For numbers and strings it asks whether they are equal, not whether they are the same object."abc" == "ab" + "c"istrue. Use==for strings; there is no need forequals.- Different number types compare by value.
5 == 5.0istrue, with anInton the left and aDoubleon the right. - Strings can be ordered.
"apple" < "banana"istrue, because strings compare alphabetically by character code. - Be careful with decimals. Doubles are stored in binary, so
0.1 + 0.2 == 0.3isfalse. To compare calculated decimals, check that the difference is tiny, as inmath.abs(x - y) < 1e-9. - A single
=is not a comparison. It assigns.if (grade = 5)doesn't compile.
Storing a comparison in a variable
Because a comparison is an ordinary expression, you can keep its result in a val and use it later, or print it directly:
val grade = 4
val failed = grade < 5
println(failed)
println(grade <= 4)
println(grade > 4)
This prints:
true
true
false
The variable failed has the type Boolean, so if (failed) works exactly like writing the comparison inside the parentheses. Naming a condition this way is useful when it is long or used more than once, and a good name such as failed or isAdult makes the if read almost like a sentence.
The exercise
The starter code gives val grade = 8. The task prints whether the student passed, following the rule that a grade below 5 is a fail.
- Write the condition. A failed test means
grade < 5. The operator<is true only when the left side is strictly smaller, so a grade of exactly 5 is not a fail. - Print inside the
if. Useprintln("Failed the test"). - Add the
else. It takes no condition and covers every grade of 5 or more, so it prints"Passed the test".
val grade = 8
if (grade < 5) {
println("Failed the test")
} else {
println("Passed the test")
}
With a grade of 8 the condition 8 < 5 is false, so the program prints Passed the test. A grade of 4 prints Failed the test, and a grade of exactly 5 prints Passed the test.
Common mistakes
- Choosing
<=instead of<. Withgrade <= 5, a grade of 5 would count as a fail. Check the boundary value whenever you pick an operator. - Swapping the sides.
5 < gradeis a different test fromgrade < 5. - Writing
=<or=>. The equal sign comes second:<=and>=. In Scala,=>means something else entirely, so it won't work as a comparison.
Key takeaways
- Comparison operators produce a
Boolean, so they fit directly in anifcondition. ==and!=test equality;<,>,<=and>=test order.- In Scala,
==compares values, strings included. - Test the boundary value to decide between
<and<=.
