The basic computer science structures of if statements, while and for loops are crucial in scientific computing. In this chapter, we cover the basics of these structures in Julia. We will see all of these in more context in later chapters.
A boolean value is something that is either true
or false
. These are built-in constants in Julia. Sometimes we will want to know if a statement is true or false, but generally, we will use them in other structures.
We often use boolean to test various conditions. For each, testing equality, or comparison of numbers we use ==,<,>,<=,>=
.
If we set x=3
and then can just type x==3
, x<3
, x>=3
to test a variety of comparisons.
We often want to test multiple boolean statements and can build up compound ones with either the “and” (&&
) or “or” (||
) operators. If we have x=3
and y=10
, if we want to test that x
is greater than 0 and y
is 5, by
x>=0 && y==5
which will return false
, since only 1 is true and both must be true for this to be true. However,
x>=0 || y==5
returns true, because the first is true. Recall the following table for && or ||:
AND | T | F | OR | T | F | |
---|---|---|---|---|---|---|
T | T | F | T | T | T | |
F | F | F | F | T | F |
An if
statement is often used inside of a procedure to do different things depending on the value of a variable. A standard example of this is the piecewise version of the absolute value. Mathematically, we write:
$$ |x| = \begin{cases} x & x \geq 0 \newline
-x & x<0
\end{cases} $$
We can make a Maple procedure for the absolute value the following way:
function absval(x::Number)
if x >= 0
return x
end
return -1*x
end
And to help determine if it is correct, evaluate absval(3)
and absval(-7)
.
Notice that we basically had two situations here, either x was greater than or equal to 0 or else not. We can rewrite this use an if-else
statement.
function absval(x::Number)
if x >= 0
return x
else
return -1*x
end
end
try entering this and seeing if the results are the same.
You may need more than 2 choices on an if statement. The following returns a color as a string for given numbers:
function which_color(n::Integer)
if n==1
return "red"
elseif n==2
return "green"
elseif n==3
return "blue"
else
return "orange"
end
end
A loop is a series of statements that are repeated either a fixed number of times or until a condition occurs. They can be very helpful if a large number of operations need to be done in a predictable manner.
Another very common construction for programming is called a while loop. Basically, we want to run a few statements while some boolean statement is true. Here’s a simple, but uninteresting example:
let
local n=1
while n<10
println(n)
n+=1
end
end
Here is a bit more practical version of this. If we want an array of integers from 1 up to some value, here’s a way to do this:
function intArray(n::Integer)
arr = []
i=1
while i<=n
push!(arr,i)
i+=1
end
arr
end
intArray(6)
returns an array from 1 to 6intArray(10)
returns an array from 1 to 10It is common in a while loop to keep running it forever. This occurs if you don’t have some code that will stop it. A couple of things:
Make sure something is changing in your loop. If you intend to stop the loop on an index, make sure the index is updating.
Look at your code and see if you have something that you think will stop the loop. What ever is in the boolean statement needs to eventually switch.
Stop the code if you need to. You may need to interrupt the kernel, but selecting Kernel from the menus at the top of the Jupyter (IJulia) webpage, then Interrupt should stop it.
If you can’t figure out why it is in an infinite loop, put print statements inside to print out values of variables.
A for loop executes some code a fixed number of times and has a variable (called an index) that updates
The following is a simple for loop that prints out the numbers 1 to 10
for i=1:10
println(i)
end
If you want to skip numbers or count backwards, the following
for i=1:2:20
println(i)
end
for i=20:-2:0
println(i)
end
and the following adds numbers in a list.
let
local sum=0
for i in [1,5,7,11,20]
sum += i
end
sum
end
Here’s the intArray
function from above using a for loop instead.
function intArray(n::Integer)
arr = []
for i=1:n
push!(arr,i)
end
arr
end
No this, isn’t a smackdown between these two. A big question often is when should I use a while
loop and when should I use a for
loop. The general rule of thumb is:
if-else
statement Often, an if-else statement is quite short and you want to return a value depending on a condition. The absolute value example was such an example. There is what is called a ternary if-else
statement that has the form:
condition ? true_condition : false_condition
which returns true_condition
if condition is true otherwise false_condition
is returned.
The absolute value example above can be written as a single line:
absval(x::Number)=(x>=0) ? x : -1*x
and once you practice with this, it will be easy to read and much shorter (1 line versus 7). alternatively, you can write this as
absval(x::Number)=ifelse(x>=0,x,-1*x)
We will see else syntax of this operator often throughout this course.