Julia Control Flow

Adarsh Ms
An Idea (by Ingenious Piece)
4 min readSep 4, 2020

if/elseif/else, ternary, boolean switching, while and for

This is part 7 of the Julia Tutorials (a series of tutorials on Julia). If you are not familiar with Julia Operators, please go through part 6 for better understanding.

If you are not familiar with Julia and want to get started with it, feel free to check out the complete series @ —

Note: Use Julia’s REPL to quickly follow along with the codes in this tutorial

Control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated.

if/elseif/else

Simple if
Used to execute a block of code if it satisfies a condition.

Syntax:

if condition
# Block of code to execute
....
end

— or —

if condition
# Block of code to execute
....
else
# Block of code to execute
....
end

Example:

if 1 == 1
println("The condition was met")
end
# With a catch-all case
if 1 == 2
println("The condition was met")
else
println("The condition was not met")
end

Ladder or branch
Used when more conditions are needed.

Syntax:

if condition
# Block of code to execute
....
elseif
# Block of code to execute
....
.
.
.
else
# Block of code to execute
....
end

Example:

if action == "play"
println("Playing the movie...")
elseif action == "resume"
println("Resuming the movie...")
elseif action == "stop"
println("Stopping the movie...")
else
println("Sorry, I'm not allowed to do this!")
end

So, for action = "resume" , output will be Resuming the movie... and for action = "nothing" , output will be Sorry, I'm not allowed to do this!

Ternary operator ?/:

The ternary operator is a useful way to reduce a reasonably simple conditional expression block into a single line of code.

Syntax:

Condition ? Expression1 : Expression2

Example:

x < 10 ? println(true) : println(false)

This yields true if x is less than 10, otherwise yields false

Equivalent of this statement using if-else is:

if x < 10
println(true)
else
println(false)
end

Boolean Switching

Boolean switching referred to as short-circuit evaluation, allows quick evaluation of expressions using boolean operators.

+----------+-----------------------------------+
| Operator | Description |
+----------+-----------------------------------+
| a || b | Execute b if a evaluates to false |
| a && b | Execute b if a evaluates to true |
+----------+-----------------------------------+

If you have gone through the previous tutorial(Operators in Julia), you may remember the logical operators || and && from there.

Example:

Using &&

isa("am i string", String) && println("Yes, you are")Output -> Yes, you are

This is equivalent to:

if isa("am i string", String)
println("Yes, you are")
end

Using ||

isa(1, String) || println("No, you are not")Output -> No, you are not

This is equivalent to:

if !isa(1, String)
println("No, you are not")
end

Note: isa() is a function that evaluates to true if a value/variable is of the specified type

While Loops

While loops are used to repeat a specific block of code an unknown number of times, until a condition is met.

Syntax:

while condition
# Block of code...
end

Example:

a = 0
b = 10
while a <= b
global a
println("Not yet...")
a += 1
end

This loop continues until a becomes greater than b .

Breaking a while loop

Sometimes, we need to terminate a loop prematurely (i.e, before it meets the condition), in that case we can use break statement.

a = 0
b = 10
while a <= b
global a
println("Not yet...")
a += 1
if a == 5
println("Breaking loop...")
break
end
end

Note: In Julia 1.0+, we need to declare scope of variables within loops, otherwise the variables will be having a local scope. So, in the above example, if you don’t specify the global scope you will get an error — UndefVarError . More about scopes will be discussed in the next part.

For Loops

For loops are used for iterating over a sequence of items (iterables).

Syntax:

for element in iterable
# Block of code to execute
....
end

Examples:

Iterating over a range

for i in 1:2:10
println("Cur value: ", i)
end

Iterating over a 1-D array

for j in ['v', 'y', 'b', 'g', 'y', 'o', 'r']
println(j)
end

Iterating over a multi-dimensional array

for elem in [1 2 3 4; 5 6 7 8; 9 10 11 12]
println(elem)
end

Iterating over a dictionary

for (key, value) in Dict('a'=>1, 'b'=>2, 'c'=>3)
println("Key: $key, Value: $value")
end

Iterating over a string (i.e, iterating over each character in a string)

for each in "Julia"
println("$each is of the type $(typeof(each))")
end

--

--

Adarsh Ms
An Idea (by Ingenious Piece)

A passionate engineer who thrives on using programming languages to converse with machines.