3. Conditionals
Boolean expressions
Section titled “Boolean expressions”A boolean expression is a statement that is either true or false.
julia> t = truetrue
julia> f = falsefalseWe can use the typeof function to check the type of a boolean variable.
julia> typeof(true)Bool
julia> typeof(false)BoolComparison operators
Section titled “Comparison operators”Comparison operators are used to compare values and return a boolean value.
Let a = 1 and b = 2. The comparison operators are:
julia> a, b = 1, 2(1, 2)
julia> a == b # equal tofalse
julia> a != b # not equal totrue
julia> a < b # less thantrue
julia> a >= b # greater than or equal tofalseExercises
Section titled “Exercises”- What is the type result returned by
1 == 2? - What is the difference between
==and===? - Make the statement
"Hello world"[i:j] == "o wo"returntrue.
Logical operators
Section titled “Logical operators”Logical operators are used to perform logical operations on boolean values.
There are three logical operators in Julia: && (AND), || (OR), and ! (NOT).
The logical operators are used to combine boolean expressions.
julia> t && f # logical ANDfalse
julia> t || f # logical ORtrue
julia> !t # logical NOTfalseFor complicated expressions, we can use parentheses to group operations.
julia> (a < b) && (b > 0) # truetrueExercises
Section titled “Exercises”-
Let
x = 5andy = 10. What is the result of the following expression?!(x > 0) && y < 0
If statements
Section titled “If statements”If statements allow you to execute code conditionally based on a boolean expression.
For example, you can use if to check if a number is greater than zero.
x = 5if x > 0 println("x is positive")endThis statement will print “x is positive” because x is greater than zero.
If x were less than or equal to zero, the code inside the if block would not execute.
Nothing happens.
Usually, you want to do something if the condition is false, so you can use else to specify an alternative block of code.
x = -5if x > 0 println("x is positive")else println("x is not positive")endThere is a third option called elseif that allows you to check multiple conditions.
Let’s use this to check if a number is positive, negative or zero.
x = 0if x > 0 println("x is positive")elseif x < 0 println("x is negative")else println("x is zero")endExercises
Section titled “Exercises”-
Write a program that prints a message based on the temperature. For example:
Below 0: “It’s freezing!”
Between 0 and 20: “It’s cold.”
Between 20 and 30: “It’s warm.”
Above 30: “It’s hot!”
Short-circuit evaluation
Section titled “Short-circuit evaluation”The logical operators && and || use short-circuit evaluation: they only evaluate the second operand when its value is needed to determine the result. This is often used as a compact alternative to if:
x = 5x > 0 && println("x is positive") # prints only when x > 0x > 0 || println("x is not positive") # prints only when x <= 0This pattern is common in Julia.
The ternary operator
Section titled “The ternary operator”The ternary operator ? : is a one-line if/else that returns a value:
x = 5sign = x > 0 ? "positive" : "non-positive"Use it for short conditional expressions; for longer logic, prefer a full if/else block.
The in operator
Section titled “The in operator”The in operator checks whether a value belongs to a collection or range:
julia> 3 in 1:10true
julia> 5 in [1, 2, 3]false
julia> 'a' in "banana"trueThis is often more readable than chaining comparisons.
Problems
Section titled “Problems”-
Complete the following program so that it prints only the even numbers between 1 and 10 that are not divisible by 4.
for i in 1:10if # Your code here. This should return `true`.println(i)endend -
Try fixing the logic in this conditional. It’s supposed to check if x is between 10 and 20, inclusive. Why doesn’t the original code work as expected?
x = 15if x > 10 || x < 20println("x is between 10 and 20")elseprintln("x is not between 10 and 20")end -
Given a wavelength
λin nanometers, write a program that prints the spectral region it falls in:- UV: 10–400 nm
- Visible: 400–700 nm
- NIR (near-infrared): 700–2500 nm
- MIR (mid-infrared): 2500–25000 nm
- Anything outside this range: “out of range”
Test it with
λ = 254(germicidal UV),532(green laser),1550(telecom), and10000(CO₂ laser).