Google News
logo
Python Program to Using the continue statement in a while loop
In the following example of Python program that demonstrates how to use the `continue` statement in a `while` loop :
Program :
# Using the continue statement in a while loop
count = 0

while count < 5:
    count += 1
    if count == 3:
        continue
    print("Count is", count)

print("Done.")
Output :
Count is 1
Count is 2
Count is 4
Count is 5
Done.
In the above program, we use a `while` loop to repeat a block of code as long as the value of `count` is less than `5`. Inside the loop, we increment the value of `count` by `1` using the `+=` operator.

We also use an `if` statement to check if the value of `count` is equal to `3`. If it is, we use the `continue` statement to skip the rest of the code in the loop and go back to the beginning of the loop.

When the value of `count` is not equal to `3`, the program continues to execute the `print()` statement, which prints the current value of `count`.

When the value of `count` reaches `5`, the loop exits, and the program continues to execute the `print()` statement outside the loop, which prints the message "Done."

You can use the `continue` statement to skip certain iterations of a loop based on a condition.