While Loop in Python

 


A while loop in Python is used to repeatedly execute a block of code as long as a given condition is True. It keeps executing the code inside the loop until the condition becomes False.

Syntax of while loop:

  • while condition:

  •     # Code block to execute as long as condition is True


  • condition: The condition is evaluated before each iteration. If it's True, the loop continues. If it's False, the loop terminates.

  • Code block: The code inside the loop is executed repeatedly as long as the condition is True.

Example 1: Basic while Loop

  • x = 0

  • while x < 5:

  •     print(x)

  •     x += 1


Output:

  • 0

  • 1

  • 2

  • 3

  • 4


In this example:

  • The while loop runs as long as the condition x < 5 is True.

  • Inside the loop, x is printed, and then x is incremented by 1 (x += 1).

Example 2: Infinite while Loop (until manually stopped)

If the condition is always True, the loop will run forever, creating an infinite loop. You can stop it manually (e.g., by using Ctrl + C).

  • while True:

  •     print("This loop runs forever!")


Note: It's important to make sure there's a condition that eventually becomes False to prevent infinite loops, unless you intend for it to run forever.

Example 3: Using break to Exit the while Loop

You can use the break statement to exit the loop early, even if the condition is still True.

  • x = 0

  • while x < 10:

  •     if x == 5:

  •         break

  •     print(x)

  •     x += 1


Output:

  • 0

  • 1

  • 2

  • 3

  • 4


In this case:

  • The loop runs until x equals 5, at which point the break statement is triggered and the loop stops.

Example 4: Using continue to Skip an Iteration

The continue statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration.

  • x = 0

  • while x < 5:

  •     x += 1

  •     if x == 3:

  •         continue  # Skip the rest of the loop when x is 3

  •     print(x)


Output:

  • 1

  • 2

  • 4

  • 5


Here:

  • When x equals 3, the continue statement is triggered, so the loop skips the print statement and moves to the next iteration.

Example 5: Using else with while Loop

Python's while loop can also have an else block, which is executed when the loop condition becomes False, but not if the loop is terminated by break.

  • x = 0

  • while x < 5:

  •     print(x)

  •     x += 1

  • else:

  •     print("Loop finished!")


Output:

  • 0

  • 1

  • 2

  • 3

  • 4

  • Loop finished!


  • The else block is executed after the loop terminates normally (i.e., when the condition becomes False).

Key Points:

  • The while loop keeps running as long as the condition evaluates to True.

  • If the condition is always True, you can create an infinite loop. Be careful with this, as it may cause the program to hang.

  • Use break to exit the loop early, and use continue to skip an iteration.

  • The else block is executed if the loop finishes naturally (without being interrupted by break).

Good Practice:

  • Ensure the condition will eventually evaluate to False to prevent infinite loops.

  • Consider using a for loop if the number of iterations is known in advance, as it can often be more readable than a while loop.

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


Post a Comment

0 Comments