A while
loop in Python is a control flow statement that repeatedly executes a block of code as long as a specified condition remains True
. It provides a way to create loops where the number of iterations is not known in advance and is determined by the condition. This makes while
loops particularly useful for scenarios where the loop should continue until a certain condition is met.
Basic syntax of a while loop
The basic syntax of a while
loop in Python is as follows:
condition
: A Boolean expression that determines whether the loop should continue running (True
) or stop (False
).
Loop initialization
Before entering a while
loop, you typically initialize one or more variables that are used within the loop. These variables are checked in the loop condition to decide whether the loop should continue or terminate.
Quiz Question
Hint: Think about the role of variables in controlling the flow of a while loop. What purpose does initializing variables before entering the loop serve in terms of managing the loop's behavior and tracking its progress?
The correct answer is C) Initialization helps in tracking the number of iterations and controlling the loop.
Loop condition
The condition specified after the while
keyword is evaluated before each iteration. If the condition is True
, the loop's block of code is executed; if the condition is False
, the loop terminates, and program execution continues after the loop.
Quiz Question
Correct!
Wrong answer. Try Again.
Please fill in all the blanks.
4
Quiz Question
Hint: Think about the timing of when the loop condition is evaluated in a while loop. Does it occur before or after each iteration?
Loop body
The block of code indented under the while
statement represents the loop body. This code is executed repeatedly as long as the condition remains True
. You should include the logic that modifies the loop control variables within this block to eventually make the condition False
and exit the loop.
Infinite loops
If the condition specified in a while
loop is never met or if there's no logic inside the loop body that can change the condition to False
, you can create an infinite loop. Infinite loops can be problematic and should be avoided unless you have a specific use case for them.
To avoid infinite loops, ensure that the loop control variables are updated correctly within the loop body.
Loop control statements
Within a while
loop, you can use control statements like break
and continue
to control the flow of the loop.
break
: Terminates the loop prematurely, regardless of whether the condition is stillTrue
.continue
: Skips the current iteration and proceeds to the next iteration of the loop.
print("Loop Finished")
Output:
Quiz Question
What is the purpose of the break statement within a while loop?
Complete example of a while loop
Let’s put everything together in a complete example:
Output:
In this example, the while
loop continues as long as the count
variable is less than 5. It prints the value of count
and increments it by 1 in each iteration until the condition count < 5
becomes False
.
To recap…
While
loops are a powerful tool in Python for executing a block of code repeatedly as long as a specified condition is true. They are particularly useful when the number of iterations is not known in advance. By understanding how to use while
loops, including loop initialization, loop conditions, loop bodies, and control statements, you can write more efficient and flexible code. Always be cautious to avoid infinite loops by ensuring that the loop control variables are properly updated within the loop body.