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 While and Do-While Loop

Mia Pivirotto
Mia Pivirotto
5,557 Points

Why does the index++ in the while and do while loops stop the program from going on forever and make it go only 5 times?

I guess I just don't understand the whole concept of the while and do while loops, what exactly are we accomplishing with them?

1 Answer

Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

"index++" is equal to write "index = index + 1". It is used like a counter to test if the cicle have to run again or not (when you test index < todo.count )

The first time the program test the condition (index < todo.count) index is equal 0.

Then within the loop index is incremented by 1 (index++).

Then the while loop starts again and test index < todo.count and index now is equal 1... and so on.

When index is equal todo.count the while loop doesn't start.