Python while loop


while Loop in Python

The while loop in Python allows you to repeatedly execute a block of code as long as a specified condition is True. It is particularly useful when you don't know the number of iterations in advance and need the loop to run until a condition changes.

Syntax

while condition: # Code block to execute while the condition is True
  • condition: The loop continues executing as long as this condition evaluates to True.
  • Once the condition becomes False, the loop terminates, and the program continues with the next statement after the loop.

How it Works

  • The while loop first checks the condition.
  • If the condition is True, it executes the code block.
  • After executing the code block, it checks the condition again.
  • This process repeats until the condition becomes False.

Example 1: Basic while Loop

i = 1 while i <= 5: print(i) i += 1

Output:

1 2 3 4 5

In this example:

  • The loop prints the value of i while i is less than or equal to 5.
  • Each time the loop runs, i is incremented by 1.
  • Once i becomes 6, the condition i <= 5 becomes False, and the loop terminates.

Example 2: Infinite Loop

If the condition in a while loop never becomes False, the loop will run indefinitely. This is known as an infinite loop.

while True: print("This loop will run forever")

To avoid infinite loops, you can include logic within the loop to change the condition or break out of the loop (using break).


Example 3: Using break to Exit the Loop

The break statement can be used to exit the loop prematurely, regardless of the condition.

i = 1 while True: print(i) i += 1 if i > 3: break

Output:

1 2 3

In this example, the loop is infinite because while True always evaluates to True. However, the break statement exits the loop once i becomes greater than 3.


Example 4: Using continue to Skip an Iteration

The continue statement is used to skip the current iteration and move to the next iteration of the loop.

i = 0 while i < 5: i += 1 if i == 3: continue print(i)

Output:

1 2 4 5

Here, the iteration where i == 3 is skipped, and the loop proceeds to the next iteration.


Example 5: else with a while Loop

An else block can be used with a while loop. The else block is executed when the condition becomes False, and the loop finishes normally. However, if the loop is terminated by a break statement, the else block is not executed.

i = 1 while i <= 3: print(i) i += 1 else: print("Loop finished")

Output:

1 2 3 Loop finished

In this example, the loop runs until i becomes 4. Once the condition becomes False, the else block is executed.

Example with break:

i = 1 while i <= 3: print(i) i += 1 if i == 2: break else: print("Loop finished")

Output:

1

In this case, the loop is terminated by the break statement when i == 2, so the else block is not executed.


Example 6: Nested while Loops

You can nest while loops inside other loops, which is useful for working with multi-level data structures like matrices.

i = 1 while i <= 3: j = 1 while j <= 2: print(f"i = {i}, j = {j}") j += 1 i += 1

Output:

i = 1, j = 1 i = 1, j = 2 i = 2, j = 1 i = 2, j = 2 i = 3, j = 1 i = 3, j = 2

In this example, the outer loop (i) runs 3 times, and the inner loop (j) runs 2 times for each iteration of the outer loop.


Summary of while Loop in Python

  • The while loop repeats a block of code as long as a specified condition is True.
  • You can control the loop using:
    • break: Exits the loop prematurely.
    • continue: Skips the current iteration and moves to the next.
  • You can use else with a while loop to execute a block of code after the loop finishes, unless interrupted by a break.
  • Be careful with infinite loops; they can occur if the condition never becomes False (e.g., while True without a break).
  • Nested while loops allow handling complex data structures or tasks with multiple levels.

The while loop is especially useful when the number of iterations is not known ahead of time, and the loop should continue until a specific condition is met.