-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlooping.jl
46 lines (36 loc) · 1.11 KB
/
looping.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using Printf
function looping_examples()
println("--------------------------------------")
println("Looping examples")
println()
println("We can use while loops to count to 20, displaying even numbers, and aborting when we get past 10")
i = 1
while i < 20
if (i % 2) == 0
println(i)
i += 1
continue # Skip the rest of the code in the loop
end
# Make the code use the global i
i += 1
if i > 10
break # Jump out of the loop
end
end
println("We can use for loops to go from one number to another, e.g. 1-5")
for i = 1:5
println(i)
end
println("We can use for loops to go from one number to another, e.g. 1-100, stepping at 10 using 1:10:100")
for i = 1:10:100
println(i)
end
println("We can use for loops to 'for-each' value in an array")
for i in [2,4,6]
println(i)
end
println("We can use for loops with multiple variables (a for-loop within a for-loop)")
for i = 1:5, j = 1:10
println((i, j))
end
end