Continue using While Loop in Python

 


The continue statement in Python is used inside loops (both for and while loops). When the continue statement is encountered, it skips the remaining code inside the loop for the current iteration and moves on to the next iteration of the loop.

How continue Works:

  • The continue statement causes the loop to stop executing the current iteration and immediately proceed to the next iteration.

  • It does not terminate the loop; it just skips the remaining part of the current iteration.

Syntax:

  • while condition:

  •     # some code

  •     if some_condition:

  •         continue  # Skip the rest of the code in this iteration

  •     # more code


Example 1: Skipping Even Numbers Using continue in a while Loop

Let's use the continue statement to skip printing even numbers in a while loop.

  • x = 0

  • while x < 10:

  •     x += 1

  •     if x % 2 == 0:  # If x is even

  •         continue     # Skip the print statement for even numbers

  •     print(x)


Output:

  • 1

  • 3

  • 5

  • 7

  • 9


Here:

  • The loop starts with x = 1, and increments x in each iteration.

  • If x is even (x % 2 == 0), the continue statement is triggered, which skips the print(x) statement.

  • Thus, only odd numbers are printed.

Example 2: Skipping Iterations Based on Multiple Conditions

In this example, we'll use the continue statement to skip iterations where the number is both divisible by 2 and divisible by 3.

  • x = 0

  • while x < 20:

  •     x += 1

  •     if x % 2 == 0 and x % 3 == 0:

  •         continue  # Skip numbers divisible by both 2 and 3

  •     print(x)


Output:

  • 1

  • 5

  • 7

  • 11

  • 13

  • 17

  • 19


Here:

  • The loop checks if a number is divisible by both 2 and 3 (x % 2 == 0 and x % 3 == 0).

  • If this condition is met, the continue statement skips the print(x) for those numbers.

Example 3: Using continue to Skip Specific Values

In this example, we'll skip the number 7 in a while loop.

  • x = 0

  • while x < 10:

  •     x += 1

  •     if x == 7:

  •         continue  # Skip printing 7

  •     print(x)


Output:

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 8

  • 9

  • 10


Here:

  • The loop checks if x is equal to 7.

  • If x is 7, the continue statement is triggered, and the loop skips the print(x) for 7.

Key Points about continue in while Loops:

  • The continue statement is useful when you want to skip certain iterations based on specific conditions, but still continue the loop.

  • It does not terminate the loop; it simply moves on to the next iteration.

  • When used in the while loop, it causes the loop to evaluate the condition again and continue the next iteration.

Good Practices:

  • Use continue when skipping specific iterations improves code readability or logic.

  • Avoid excessive use of continue if it complicates the flow of the loop. Keep your loops clear and easy to understand.

Let me know if you want more examples or need further explanations! 😊


Post a Comment

0 Comments