Introduction to the Julia programming language

8 Control Flow¶

if then else¶

Conditional execution is very easily achieved with an if block in Julia:

In [1]:
function gtlteq(a, b)
    if a > b
        println("$a is greater than $b")
    elseif a < b
        println("$a is less than $b")
    elseif a == b
        println("$a is equal to $b")
    else
        println("I have no idea about $a and $b!")
    end
end
gtlteq (generic function with 1 method)
In [2]:
gtlteq(2.3, -9)
2.3 is greater than -9
In [3]:
gtlteq("apples", "oranges")
apples is less than oranges

Short Circuit and Ternary Operators¶

Short Circuit¶

The operators && and || can be used for conditional execution.

  • a && b - evaluate b only if a (i.e., a is true)
  • a || b - evaluate b only if !a (i.e., a is false)
In [4]:
true && "it's true"
"it's true"
In [5]:
false || "it's false"
"it's false"
In [6]:
false && "it's true"
false

Ternary¶

A special type of "quick" conditional is the ternary operator, familiar from C:

In [7]:
2 > 4 ? "it's bigger" : "it's smaller"
"it's smaller"

The syntax is a ? b : c meaning if a, evaluate b, else evaluate c.

Loops¶

For loops¶

For loops are always over an iterator in Julia, allowing a loop over any object that has a beginning and an end:

In [8]:
for number in 1:4
    println("I am at number $number")
end
I am at number 1
I am at number 2
I am at number 3
I am at number 4

enumerate¶

In [9]:
for (index, string) in enumerate(["I", "think", "therefore", "I", "am"])
    println("Word $index is $string")
end
Word 1 is I
Word 2 is think
Word 3 is therefore
Word 4 is I
Word 5 is am

zip¶

In [10]:
for (hip, hop, hup) ∈ zip(1:3, 10:12, ["yes", "no", "maybe"])
    println("The $hip and $hop say $hup")
end
The 1 and 10 say yes
The 2 and 11 say no
The 3 and 12 say maybe

Note that Julia is happy with for z in ... and for z ∈ ... (type \in to get a ∈; note that = can be used, so you might see this, but it is frowned upon!)

A more novel feature: for will also naturally form the outer product of a comma-separated set of iterators provided to them, without the need for "nesting" of loops. (This also applies to the for in comprehensions, so you can easily make multi-dimensional arrays with a chain of iterators if you want all the combinations that result as elements.)

In [11]:
for i in 1:3, j in 1:4  #like for i=1:3 ; for j=1:4 ...
    println("The product of $i and $j is $(i*j)")
end
The product of 1 and 1 is 1
The product of 1 and 2 is 2
The product of 1 and 3 is 3
The product of 1 and 4 is 4
The product of 2 and 1 is 2
The product of 2 and 2 is 4
The product of 2 and 3 is 6
The product of 2 and 4 is 8
The product of 3 and 1 is 3
The product of 3 and 2 is 6
The product of 3 and 3 is 9
The product of 3 and 4 is 12

while loops¶

By now it is going to be no surprise to you how Julia constructs a while loop:

In [12]:
countdown = 5
while countdown > 0
    println(countdown, "...")
    countdown -= 1
end
println("blast off!")
5...
4...
3...
2...
1...
blast off!

break¶

In [26]:
start, stop = 1024, 1200

# find first number between start and stop which is divisible by 31
for i in start:stop
    if i % 31 == 0
        println("The first number divisible by 31 \
                 in the range $start to $stop is: $i")
        break  # exit once we find the first number divisible by 31
    end
end
The first number divisible by 31 in the range 1024 to 1200 is: 1054

continue¶

In [21]:
# print numbers divisible by 31 between start and stop
for i in start:stop
    if i % 31 != 0
        continue
    end
    println(i)
end
1054
1085
1116
1147
1178