Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
The continue
statement is a control flow statement in Python that allows you to skip the rest of the current iteration in a loop (i.e., for
or while
loop) and proceed to the next iteration. The continue
statement is often used in situations where you want to skip specific elements in a sequence and continue processing the remaining elements.
In this tutorial, we will discuss the usage of the continue
statement in Python with examples.
continue
in a for
loop:When the continue
statement is encountered inside a for
loop, the current iteration is terminated, and the loop proceeds with the next iteration.
Example:
for i in range(1, 6): if i == 3: continue print(i)
Output:
1 2 4 5
In this example, the for
loop iterates over the numbers from 1 to 5. When i
is equal to 3, the continue
statement is executed, and the current iteration is skipped. The numbers 1, 2, 4, and 5 are printed, but the number 3 is not.
continue
in a while
loop:Similar to the for
loop, the continue
statement can be used inside a while
loop to skip the current iteration and proceed to the next iteration.
Example:
counter = 1 while counter <= 5: if counter == 4: counter += 1 continue print(counter) counter += 1
Output:
1 2 3 5
In this example, the while
loop continues as long as counter
is less than or equal to 5. When counter
is equal to 4, the continue
statement is executed, and the current iteration is skipped. The numbers 1, 2, 3, and 5 are printed, but the number 4 is not.
continue
with nested loops:When using nested loops (i.e., a loop inside another loop), the continue
statement will only affect the innermost loop in which it is placed.
Example:
for i in range(1, 4): for j in range(1, 4): if j == 2: continue print(f"i: {i}, j: {j}")
Output:
i: 1, j: 1 i: 1, j: 3 i: 2, j: 1 i: 2, j: 3 i: 3, j: 1 i: 3, j: 3
In this example, the continue
statement is inside the inner for
loop. When j
is equal to 2, the inner loop skips the current iteration, but the outer loop continues to execute. As a result, the output only shows the values of i
and j
when j
is not equal to 2.
In conclusion, the continue
statement is a control flow statement in Python that allows you to skip the rest of the current iteration in a loop and proceed to the next iteration. The continue
statement is useful in situations where you want to skip specific elements in a sequence and continue processing the remaining elements.
Skipping iterations with continue
in Python:
continue
statement is used to skip the rest of the code in the current iteration and move to the next iteration of the loop.for i in range(5): if i == 2: continue print(i)
Conditional use of continue
in Python loops:
continue
can be used conditionally to skip iterations based on certain conditions.numbers = [1, 2, 3, 4, 5] for num in numbers: if num % 2 == 0: continue print(num)
Using continue
with for
loops in Python:
continue
is commonly used with for
loops to skip iterations and move to the next one.for char in "Python": if char == "h": continue print(char)
Breaking out of inner loops with continue
:
continue
can be used to skip iterations in inner loops without exiting the entire loop.for i in range(3): for j in range(3): if j == 1: continue print(i, j)
Common patterns for using continue
in Python:
continue
is often used to skip iterations for specific conditions while processing elements in a loop.for number in range(10): if number % 3 == 0: continue print(number)
continue
statement vs break
statement in Python:
continue
skips the rest of the code in the current iteration and moves to the next one, while break
exits the entire loop.for i in range(5): if i == 2: continue print(i) if i == 3: break
Handling specific conditions with continue
in loops:
continue
can be used to handle specific conditions by skipping unnecessary code execution.for person in people: if person["age"] < 18: continue process_adult(person)
Using continue
with while
loops in Python:
continue
can also be used with while
loops to skip the rest of the code in the current iteration.count = 0 while count < 5: count += 1 if count == 3: continue print(count)