Python From Beginner to Advanced

0% completed

Previous
Next
Python - continue Statement

The continue statement in Python is used to skip the current iteration of a loop and move to the next one. Unlike break, which completely stops the loop, continue allows the loop to keep running while skipping specific iterations based on a condition.

Syntax

for variable in sequence: if condition: continue # Skips the rest of the loop body for this iteration # Other statements inside the loop while condition: if condition: continue # Skips the rest of the loop body for this iteration # Other statements inside the loop
  • continue works in both for and while loops.
  • When Python encounters continue, it jumps back to the start of the loop, skipping any code below it in the current iteration.

Examples

Example 1: Skipping a Number in a "for" Loop

Python3
Python3

. . . .

Explanation

  1. The loop runs from 1 to 5.
  2. When num is 3, the if condition is True, so continue skips printing 3 and moves to the next iteration.
  3. In the output, numbers 1, 2, 4, and 5 will be printed, but 3 will be skipped.

Example 2: Skipping Even Numbers in a "while" Loop

Python3
Python3

. . . .

Explanation

  1. The loop runs from 1 to 10, increasing count in each iteration.
  2. If count is even, continue skips printing that number.
  3. In the output, only odd numbers (1, 3, 5, 7, 9) will be printed.

The continue statement is used to skip specific iterations of a loop without stopping the entire loop. It allows the program to ignore certain conditions while still executing the rest of the loop.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next