Using loops in Python automates and repeats tasks efficiently. But sometimes a condition may arise where you want to exit the loop completely, skip iteration, or ignore this condition. This can be done using loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects created in that scope are destroyed. Python supports the following control statements.
In this article the focus will be on the
Continue is also a loop control statement, just like the break statement. The
continue statement is the opposite of the
break statement, instead of terminating the loop, it forces the next iteration of the loop to run.
As the name suggests, the continue statement causes the loop to continue or to perform the next iteration. When the continue statement is executed in a loop, the code within the loop following the continue statement will be skipped and the next iteration of the loop will begin.
Consider the situation when you need to write a program that prints a number between 1 and 10, not 6. It is specified that you should do this with a loop, and only one loop is allowed.
This is where the
continue statement is used. Here we can start a loop from 1 to 10, and every time we need to compare the value of the iterator with 6. If it is 6, we will use the continue statement to go to the next iteration, without printing anything else, we will print the value. P >
Below is the implementation of the above idea:
1 2 3 4 5 7 8 9 10
continue statement can be used with any other loop, for example, at the same time as
for loop described above.
Given the number n, print a triangular pattern. We are only allowed to use one loop.
Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * pre >