Table of Contents
Python Continue Statement will ignore the execution of the Program and will block after the continue statement and then force the control to begin the next iteration. This article has listed the Python Continue Statement.
What is Python Continue Statement
Python Continue Statement is referred to as a Loop control statement that can force to stop the next iteration of the loop while neglecting the rest of the code that appears inside the loop for the current iteration.
However, the continue statement will function for both while and loops
What is the Syntax of Continue Statement
while True:
...
if x == 10:
continue
print(x)
What are the examples of Python Continue Statement?
Example 1: Python Continue Statement within a Loop
# Example of using continue within a loop
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
# Skip the even numbers and continue to the next iteration
continue
print(f"Processing odd number: {num}")
Output
Processing odd number: 1
Processing odd number: 3
Processing odd number: 5
Example 2: Continue Statement With Nested Loops
# Example of using continue within nested loops
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
# Skip the rest of the inner loop for j == 2
continue
print(f"Outer loop: {i}, Inner loop: {j}")
Output
Outer loop: 1, Inner loop: 1
Outer loop: 1, Inner loop: 3
Outer loop: 2, Inner loop: 1
Outer loop: 2, Inner loop: 3
Outer loop: 3, Inner loop: 1
Outer loop: 3, Inner loop: 3
Example 3: Continue Statement with While Loop
# Example of using continue with a while loop
i = 1
while i <= 5:
if i == 3:
# Skip the rest of the code inside the loop for i == 3
i += 1 # Don't forget to increment i to avoid an infinite loop
continue
print(f"Current value of i: {i}")
i += 1
Output
Current value of i: 1
Current value of i: 2
Current value of i: 4
Current value of i: 5
What are the uses of the Continue Statement?
Loops in Python will automate and can repeat the tasks more efficiently. Whereas, a condition may occur when you need to exit the loop and skip the iteration.
Further, this process can be performed with the help of a loop control statement. The continue Statement is referred to as a type of loop control statement that can change the flow of the loop.
Conclusion
In Python, the continue statement acts like a skip button for loops, allowing you to effortlessly bypass certain iterations. When faced with specific conditions, continuing enables you to jump to the next iteration, ignoring the rest of the loop code.
However, It’s considered as a powerful tool for fine-tuning loops, optimizing your code, and ensuring seamless execution.
Python Continue Statement- FAQs
Q1. What is the difference between pass break and continue in Python?
Ans. The break statement will stop the entire loop as soon as the condition is found. In contrast, the continue statement will ignore the current iteration and will move to the next one.
Q2. What is the continue loop command in Python?
Ans. The ‘while True’ Statement will function to make the infinite loop in Python.
Q3. What is the syntax of continue?
Ans. The syntax of continue is continue;
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together