In python, continue statement is useful to skip the execution of the current iteration of the loop and continue to the next iteration.
The main difference between break
and continue
statements are the break
statement will completely terminate the loop, and the program execution will move to the statements after the body of the loop, but the continue
statement skips the execution of the current iteration of the loop and continue to the next iteration.
Following is the syntax of defining the continue statement in python.
Following is the flow chart diagram of the continue statement in python.
If you observe the above continue statement flow chart diagram, the execution of code inside a loop will be skipped for a particular iteration when the continue statement is executed.
In python, you can use the continue statement in for loop to skip the current iteration of the loop and continue to the next iteration.
Following is the example of using the continue statement in python for loop to skip the execution of a particular iteration and pass control to the next iteration of the loop based on the defined condition.
When you execute the above python program, you will get the result as shown below.
If you observe the above result, the for loop has skipped the execution for 3rd item iteration and continued to the next iteration.
Like python for loop, you can also use continue statement in a while loop to skip the execution of the current iteration of the loop and continue to the next iteration.
Following is the example of using a continue statement in python while loop to skip the execution of a particular iteration and pass control to the next iteration of the loop based on the defined condition.
When you execute the above python program, you will get the result as shown below.
If you observe the above result, the while loop has skipped the 3rd item iteration execution and continued to the next iteration.
This is how you can use continue statement in python for, and while loops to skip the execution of a particular iteration and pass control to the loop's next iteration based on your requirements.