November 2017
Beginner
316 pages
6h 40m
English
While coding, we are often confronted with code that has something to do with the range of items to loop upon. For instance, how does someone know whether any number between 1 and 10 is either even or odd? This is very simple, but how do we start?
The following code shows how easily we can do this:
julia> numbers = [1,2,3,4,5,6,7,8,9,10]
10-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10
julia> for n in numbers
if rem(n,2) == 0
println("$n is even")
end
end
2 is even
4 is even
6 is even
8 is even
10 is even
Simple! But wait - what if we had 100 numbers? Will we go on by first creating an array of 100 numbers? No. We will then use range expression. Let's see that in action:
julia> for n in 1:10 if rem(n,2) == 0 println("$n is even") ...Read now
Unlock full access