Control flow

Author

Marie-Hélène Burle

Control flow statements alter the linear execution of code, allowing for one or another section of code to be executed, or for one section of code to be executed multiple times.

Conditional statements

Conditional statements allow to run instructions based on predicates: different sets of instructions will be executed depending on whether the predicates return true or false.

Predicates

Here are a few examples of predicates with classic operators:

occursin("that", "this and that")
4 < 3
a == b
a != b
2 in 1:3
3 <= 4 && 4 > 5
3 <= 4 || 4 > 5

In addition, Julia possesses more exotic operators that can be used in predicates:

  • The inexact equality comparator, useful to compare floating-point numbers despite computer rounding.

The function isapprox or the equivalent binary operator (typed with \approx<tab>) can be used:

0.1 + 0.2 == 0.3
false
0.1 + 0.2  0.3
true
isapprox(0.1 + 0.2, 0.3)
true

The negatives are the function !isapprox and (typed with \napprox<tab>).

  • The equivalent or triple equal operator compares objects in deeper ways (address in memory for mutable objects and content at the bit level for immutable objects).

=== or (typed with \equiv<tab>) can be used:

a = [1, 2]; b = [1, 2];
a == b
true
a  b     # This can also be written `a === b`
false
a  a
true

If statements

if <predicate>
    <some action>
end
  • If <predicate> evaluates to true, the body of the if statement gets evaluated (<some action> is run),
  • If <predicate> evaluates to false, nothing happens.

Example:

function testsign1(x)
    if x >= 0
        println("x is positive")
    end
end
testsign1 (generic function with 1 method)
testsign1(3)
x is positive
testsign1(-2)

Nothing gets returned since the predicate returned false.

If else statements

if <predicate>
    <some action>
else
    <some other action>
end
  • If <predicate> evaluates to true, <some action> is done,
  • If <predicate> evaluates to false, <some other action> is done.

Example:

function testsign2(x)
    if x >= 0
        println("x is positive")
    else
        println("x is negative")
    end
end
testsign2 (generic function with 1 method)
testsign2(3)
x is positive
testsign2(-2)
x is negative

If else statements can be written in a terse format using the ternary operator:

<predicate> ? <some action> : <some other action>

Here is our function testsign2 written in terse format:

function testsign2(x)
    x >= 0 ? println("x is positive") : println("x is negative")
end

testsign2(-2)
x is negative

Here is another example:

a = 2
b = 2.0

if a == b
    println("It's true")
else
    println("It's false")
end

And in terse format:

a == b ? println("It's true") : println("It's false")
It's true

If elseif else statements

if <predicate1>
    <some action>
elseif <predicate2>
    <some other action>
else
    <yet some other action>
end

Example:

function testsign3(x)
    if x > 0
        println("x is positive")
    elseif x == 0
        println("x is zero")
    else
        println("x is negative")
    end
end
testsign3 (generic function with 1 method)
testsign3(3)
x is positive
testsign3(0)
x is zero
testsign3(-2)
x is negative

Loops

For loops

For loops run a set of instructions for each element of an iterable:

for <iterable>
    <some action>
end

Examples:

for name = ["Paul", "Lucie", "Sophie"]
    println("Hello $name")
end
Hello Paul
Hello Lucie
Hello Sophie
for i = 1:3, j = 3:5
    println(i + j)
end
4
5
6
5
6
7
6
7
8

While loops

While loops run as long as a condition remains true:

while <predicate>
    <some action>
end

Example:

i = 0

while i <= 10
    println(i)
    i += 1
end
0
1
2
3
4
5
6
7
8
9
10