Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
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.