Continue In Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 43:- Continue In Python

In Python, the continue statement is used to skip the rest of the current iteration of a loop and proceed to the next iteration. When the continue statement is encountered inside a loop (e.g., for or while loop), the remaining code in that iteration is skipped, and the loop moves to the next iteration to continue its execution.

The continue statement is often used in combination with conditional statements (if, else, elif) to control when to skip the current iteration.

Here's an example of how the continue statement works in a for loop:

pythonCopy code

for num in range(1, 6):    if num == 3:        continue    print(num)

Output:

Copy code

1 2 4 5

In this example, the for loop iterates over the numbers from 1 to 5 using the range() function. When the value of num becomes 3, the if statement checks the condition num == 3, which evaluates to True. As a result, the continue statement is executed, causing the rest of the code inside the loop to be skipped for the current iteration. Therefore, the number 3 is not printed, and the loop proceeds to the next iteration, printing the other numbers.

The continue statement can also be used with a while loop to skip the current iteration based on specific conditions.

Example using continue with a while loop:

pythonCopy code

count = 0 while count < 5:    count += 1    if count == 3:        continue    print("Current count:", count)

Output:

sqlCopy code

Current count: 1 Current count: 2 Current count: 4 Current count: 5

In this example, the while loop iterates while count is less than 5. When the value of count becomes 3, the if statement with continue is executed, causing the rest of the code inside the loop for that iteration to be skipped. Therefore, the number 3 is not printed, and the loop proceeds to the next iteration.

The continue statement is useful when you want to skip specific iterations of a loop based on certain conditions. It allows you to control the flow of your program more precisely and avoid executing unnecessary code.

6. Loops

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?