Introduction to the Julia programming language
8 Control Flow¶
if then else¶
Conditional execution is very easily achieved with an if
block in Julia:
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)
gtlteq(2.3, -9)
2.3 is greater than -9
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
- evaluateb
only ifa
(i.e.,a
is true)a || b
- evaluateb
only if!a
(i.e.,a
is false)
true && "it's true"
"it's true"
false || "it's false"
"it's false"
false && "it's true"
false
Ternary¶
A special type of "quick" conditional is the ternary operator, familiar from C:
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
.
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
¶
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
¶
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.)
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:
countdown = 5
while countdown > 0
println(countdown, "...")
countdown -= 1
end
println("blast off!")
5... 4... 3... 2... 1... blast off!
break
¶
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
¶
# 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