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
Alex Rendon
7,498 PointsA little question about "continue" in the loop "while".
What happens if I put a "continue" in a while loop.
I believe that the "continue" reset the loop and start again the code in the while.
Is it okay?
1 Answer
Unsubscribed User
3,801 PointsYou are correct. Using continue in a while loop will go to the next iteration of the loop, ignoring any code blocks after the continue.
For example:
counter = 5
while counter > 0:
counter = counter -1
if counter == 3:
continue
print 'Current count:', counter
This code will count down from 5 to 0 and print the current number. However, if the number is 3, the continue statement will skip the print and go to the next number. The output looks like this:
Current count: 4
Current count: 2
Current count: 1
Current count: 0
Using continue in a while (or a for) loop is valid syntax, and is OK to do.
Alex Rendon
7,498 PointsAlex Rendon
7,498 PointsOk Mark, thanks so much. This is a big help.