SS22 Scientific Coding with Julia
Conditional evaluations
In the following, we start discussing control flows. Let us start with conditional statements. Recall that Julia provides booleans, that is, variables that can be either true
or false
. Often, an algorithm wants to react to a statement being true
or false
. As an example, we would like to check if a given input is correct and if it is not, print a warning message. This sentence already shows that we need an if
statement, which takes the form
if condition
# code to be run if condition is true
end
As an example, let us choose a number and check if this number is stored as a Floating Point Number (or float for short) or an Integer
number = 2
if number isa Integer
println("This number is an integer")
end
This number is an integer
Now if this number is not an integer, we would like to print out this information. This can be done with the else
statement
number = 2.0
if number isa Integer
println("This number is an integer")
else
println("This number is not an integer")
end
This number is not an integer
Moreover, if we want to check whether the number is a float in case it is not an integer we can use the elseif
statement
number = 2.0
if number isa Integer
println("This number is an integer")
elseif number isa AbstractFloat
println("This number is a float")
else
println("This number is neither a float nor an integer")
end
This number is a float
Note that in comparison to other programming languages, if
blocks can define new variables which can be used outside of these blocks.
number = 2.0
if number isa Integer
info = "This number is an integer"
else
info = "This number is not an integer"
end
println(info)
This number is not an integer
CC BY-SA 4.0 - Gregor Ehrensperger, Peter Kandolf, Jonas Kusch. Last modified: September 09, 2022.
Website built with Franklin.jl and the Julia programming language.