Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Control Flow For-Condition-Increment

Arun Sivananthan
Arun Sivananthan
1,847 Points

For loop?

for var i = 0; i < todo.count; i++ { println(todo[i]) }

If i is incremented before the println function won't the first thing printed be from the second position or index 1 rather than 0?

2 Answers

Jeff Lyons
Jeff Lyons
7,243 Points

With that For loop syntax, the increment (or decrement, as the case may be) doesn't happen until AFTER the condition in the middle has been deemed true and the actions inside the braces executed. Someone else may be able to give you a more detailed explanation, but this is my understanding:

Step 1: initialize variable (i)

Step 2: test condition (Is i less than the total number of array elements?)

Step 3: If Step 2 is YES, execute instructions inside braces and proceed to Step 4. If NO, terminate loop.

Step 4: increment i and go to Step 2