Reputation: 33
I didn't understood the concept of the For-In loop in swift 3 , can anyone explain to us it m thanks in advance
var total = 0
for i in 0..<4 {
total += i
}
print(total)
The result of total is 6 , Why ?
Upvotes: 0
Views: 50
Reputation: 1589
Your loop will be vary from 0 to 3 i.e. 0,1,2,3 but if you want it will vary from 0 to 4 then try this -
var total = 0
for i in 0...4 {
total += i
}
print(total)
Upvotes: 0
Reputation: 56
i=0 => total = 0+0 =0
i=1 => total = 0+1 = 1
i=2 => total = 1+2 = 3
i=3 => total = 3+3 =6
it's simply alogrithm ;-)
i never reach 4 because you said it STRICTLY inferior to 4 =)
(Do I answer your question?)
Upvotes: 1