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

Python Python Basics (Retired) Shopping List Shopping List Project

Why was "continue" written at the end of the while loop?

He made it seem like the loop would have not started over, had "continue" not been written. Was that the case?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, that continue could have been left off. Including it, though, serves two purposes. 1) It gives me a good way of illustrating the keyword and concept. 2) It makes the continuation of the loop more explicit.

Sebastian Rรถder
Sebastian Rรถder
13,878 Points

I do not agree that it helps the learner to understand the concept of break/continue when it is used in places, where it does not actually change the normal control flow of the program. Unfortunately there where a couple of examples in the code of this course, where it was used like this. I would consider this a code smell/bad style that makes it harder to understand the code and may lead to bugs when the code has to be modified some day.

From my point of view, the learner should:

  1. learn to reason about the normal control flow of loops, conditionals etc.
  2. learn how to alter this control flow in certain cases with break/continue (without overusing it)

I would appreciate if this could be improved over time in the course.

3 Answers

Stone Preston
Stone Preston
42,016 Points

as others have said continue skips over everything after it and goes back to the start of the loop. if it was at the very end of the loop I dont think it was necessary to write the continue statement. it would have gone back to the beginning of the loop regardless.

x = 0
while x < 10:
   print(x)
   x = x + 1
   continue

will do the same as

x = 0
while x < 10:
   print(x)
   x = x + 1

however if you had some code after a continue statement that would be skipped over and then loop would start over

x = 0
while x < 10:
   print(x)
   x = x + 1
   continue
   print("This statement skipped over since it occurs after the continue statement"
Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

how about something like,

while:
  if:
    break
  elif:
    continue

in cases continue can be necessary at the end of loop?

Stone Preston
Stone Preston
42,016 Points

I dont think the continue there after the else make a difference in that case there either. if you have no code after the continue it doesnt really do anything since the loop is going to start over anyways.

After reading and testing, it's more of a "go back" than a "continue" and it can be used to go back to the beginning of the loop if some condition not desired for continuing is presented. According to stackoverflow, it helps readability and to avoid deeply nested loops.

while True:
    if condition == True:
        continue
    if other_condition == True:
        continue
    doSomething()    #  If both previous conditions have failed
    break
Stone Preston
Stone Preston
42,016 Points

yeah naming the keyword continue makes things a bit confusing.

Christopher Johnson
seal-mask
.a{fill-rule:evenodd;}techdegree
Christopher Johnson
Python Web Development Techdegree Student 11,422 Points

Yes. The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

Yes, as like break quits in the middle of the loop, it seems continue goes to the very beginning of the loop without going through the rest of it.