Malik Türk
Malik Türk

Reputation: 33

index in a loop by using ..<

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

Answers (2)

Ved Rauniyar
Ved Rauniyar

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

Tristan
Tristan

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

Related Questions