Python While Loop

This topic is to discuss the following lesson:

counter = 0

while counter < 3:
    counter += 1
    if counter == 2:
        print("This is never executed")
    print("Counter value is %s" % counter)

Should not it print “This is never executed” when counter values is 2 during the incrementing counter value loop?

Hello Rupak

The continue statement is used to stop the current iteration and continue with the next one. This means that:

  • when the counter is equal to 2, the continue statement is executed.
  • the loop stops there and goes back to the beginning without executing the next statements

Therefore, the print("This is never executed") statement is never executed.

I hope this has been helpful!