Programming in Swift · For Loops | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/13

Hi RW team & forum!

Quick question about constants: at 4:03 in the For Loops video, you mention that the for loops’ index i is a constant.

The Swift Documentation for Control Flow mentions this as well.

Isn’t i changing values (1, then 2, then 3, etc.) as the for loop executes? If so, it seems odd that i would be considered a constant.

Any notes on this, or is it important to know why this is the case?

Thanks in advance, and keep up the great videos!

" index is a constant whose value is automatically set at the start of each iteration of the loop."

The scope of index is the body of the loop. For each pass, it is in effect a new constant, which is set to the value for that pass, and which goes out of scope at the end of the pass.

1 Like

Ah, forgot about scope—and that scope makes sense as i is not needed outside of the for loop

Thank you for answering!