Loop Construct – Continue
The continue
statement is a control flow statement in programming languages that allows you to skip the current iteration of a loop and move on to the next iteration, without executing the rest of the code inside the loop for the current iteration.
The continue
statement is commonly used inside loops that iterate over a sequence of values, such as the for
and while
loops. When the continue
statement is encountered inside a loop, the program skips the remaining statements in the loop block for the current iteration, and jumps directly to the next iteration of the loop.
Here is an example of using the continue
statement in Python to print only the odd numbers from a list of integers:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]for num in numbers:
if num % 2 == 0:
continue
print(num)
In this example, the for
loop iterates over each element in the numbers
list, and the if
statement checks if the current element is even (divisible by 2). If the element is even, the continue
statement is executed, and the program skips the remaining statements in the loop block and moves on to the next iteration. If the element is odd, the print
statement inside the loop block is executed, and the odd number is printed to the console.
The continue
statement can be useful for skipping over certain values or conditions in a loop, and for simplifying complex loop logic. However, it should be used judiciously, as excessive use of continue
statements can make the code harder to read and understand.
Apply for PHP Certification!
https://www.vskills.in/certification/certified-php-developer