Julia Comparison Operators
A comparison operator compares two values and gives a Bool result. The result is true when the comparison holds and false when it does not.
Comparing numbers
Each comparison prints one Bool value. Read each line and check the result against the two numbers.
println(3 == 3)
println(3 != 5)
println(2 < 5)
println(7 >= 9)Chained comparisons
Julia can check a whole chain at once. The chain is true only when every single link holds.
println(1 < 2 < 3)
println(10 <= 10 <= 20)
println(5 > 4 > 10)Comparing strings
Strings compare in alphabetical order. A word that comes earlier in the alphabet is the smaller one.
println("apple" == "apple")
println("apple" < "banana")
println("cat" > "dog")Example
score = 85
passmark = 60
println(score > passmark)
println(score == 100)
println(0 <= score <= 100)
println(score < 50)Key points
- Equal: == asks whether two values are equal.
- Not equal: != asks whether two values are different.
- Less and greater: < means smaller than and > means bigger than.
- Or equal: <= means smaller than or equal to, and >= means bigger than or equal to.
- Chained comparison: a chain such as 1 < 2 < 3 is true only when every link in the chain holds.
- String comparison: text compares alphabetically, letter by letter.
In this example, score holds 85 and passmark holds 60. The first line checks that 85 is bigger than 60, the second checks whether the score is exactly 100, the third uses a chain to check the score sits between 0 and 100, and the last checks whether it is below 50.
Note
a comparison gives a Bool, so its printed result is always true or false.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
What does println(4 <= 4) print?