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
condition
: The loop continues executing as long as this condition evaluates toTrue
.- 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
Output:
In this example:
- The loop prints the value of
i
whilei
is less than or equal to 5. - Each time the loop runs,
i
is incremented by 1. - Once
i
becomes 6, the conditioni <= 5
becomesFalse
, 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.
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.
Output:
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.
Output:
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.
Output:
In this example, the loop runs until i
becomes 4. Once the condition becomes False
, the else
block is executed.
Example with break
:
Output:
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.
Output:
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 isTrue
. - 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 awhile
loop to execute a block of code after the loop finishes, unless interrupted by abreak
. - Be careful with infinite loops; they can occur if the condition never becomes
False
(e.g.,while True
without abreak
). - 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.