4. Iteration
While statements
Section titled “While statements”While loops allow you to execute a block of code as long as a condition is true.
The code block below will print the numbers from 1 to 10 by incrementing a variable i by 1 with each iteration.
n = 5while n > 0 println(n) n = n - 1endprintln("Blast off!")While loops can be tricky because if the condition never becomes false, the loop will run indefinitely.
while true println("This will run forever!")endFor statements
Section titled “For statements”For loops allow you to iterate over a range of values or elements in a collection.
fruits = ["apple", "banana", "cherry"]for fruit in fruits println(fruit)endIn Julia, the : operator can be used to create ranges.
For example, 1:5 creates a range from 1 to 5.
Let’s use this notation to print the numbers from 1 to 10.
for i in 1:10 println(i)endYou can also specify a step between the start and stop using start:step:stop:
for i in 1:2:10 println(i) # prints 1, 3, 5, 7, 9end
for x in 0.0:0.1:1.0 println(x) # prints 0.0, 0.1, ..., 1.0endbreak and continue
Section titled “break and continue”You can exit a loop early with break, or skip ahead to the next iteration with continue.
for i in 1:10 if i > 5 break # stops the loop entirely end println(i)end
for i in 1:10 if iseven(i) continue # skip this iteration end println(i) # prints only odd numbersendIterating with an index
Section titled “Iterating with an index”Sometimes you need the index as well as the value while iterating. Julia has a few helpers for this.
eachindex returns the valid indices of a collection. Prefer it over 1:length(x) — it works for arrays that don’t start at index 1 and is generally safer.
v = [10.0, 20.0, 30.0]for i in eachindex(v) println("v[$i] = $(v[i])")endenumerate pairs each element with its position:
for (i, value) in enumerate(v) println("position $i: $value")endzip lets you iterate over two collections in parallel:
wavelengths = [400, 500, 600]intensities = [0.2, 0.9, 0.5]for (λ, I) in zip(wavelengths, intensities) println("λ = $λ nm, I = $I")endList comprehensions
Section titled “List comprehensions”A comprehension is a compact way to build a vector by transforming each element of a range or collection. The syntax mirrors set-builder notation in mathematics.
julia> squares = [i^2 for i in 1:5]5-element Vector{Int64}: 1 4 9 16 25You can filter elements with an if clause:
julia> evens = [i for i in 1:10 if iseven(i)]5-element Vector{Int64}: 2 4 6 8 10Comprehensions can use any expression on the left of for, including function calls. They are a Julia idiom — you will see them everywhere.
Problems
Section titled “Problems”-
Use a
forloop to compute the sum of all integers from 1 to 100. (The total should be 5050.) -
Use a list comprehension to build a vector containing the squares of the integers from 1 to 10.
-
Given the vector below, write a loop that finds the index where the value is largest. (Don’t use
argmax.)y = [0.1, 0.3, 0.5, 1.2, 0.8, 0.4, 0.2]# Your code here. Expected result: 4. -
A moving average smooths a noisy signal by averaging neighboring points. Given a vector
yand a window sizew, compute a new vector whosei-th element is the mean ofy[i:i+w-1]. Test it on:y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]w = 3# Expected result: [2.0, 3.0, 4.0, 5.0, 6.0]