The while
loop is a control structure used in programming to repeat a block of code as long as a specified condition remains true. It is particularly useful when you don’t know beforehand how many times you need to repeat a block of code.
Syntax
while condition: # Code to execute repeatedly
- condition: This is an expression that is evaluated before each iteration of the loop. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop stops.
How It Works
- Condition Evaluation: Before each iteration, the condition is checked.
- Code Execution: If the condition is true, the code inside the loop is executed.
- Repeat: After executing the code block, the condition is evaluated again. If it is still true, the loop continues; otherwise, the loop terminates.
Example 1: Basic While Loop
count = 0while count < 5: print("Count is:", count) count += 1
Output:
Count is: 0Count is: 1Count is: 2Count is: 3Count is: 4
Explanation:
- The loop starts with
count = 0
. - The condition
count < 5
is true, so the loop prints the current value ofcount
and then increments it by 1. - This process repeats until
count
reaches 5, at which point the conditioncount < 5
becomes false, and the loop terminates.
Example 2: Infinite Loop
If the condition never becomes false, the loop will run indefinitely. This is called an infinite loop.
while True: print("This will run forever!")
Explanation:
- The condition
True
is always true, so this loop never ends. You typically usebreak
to exit such loops manually.
Example 3: Using break
to Exit a Loop
The break
statement can be used to exit a loop prematurely, even if the condition is still true.
count = 0while count < 10: print("Count is:", count) if count == 5: break count += 1
Output:
Count is: 0Count is: 1Count is: 2Count is: 3Count is: 4Count is: 5
Explanation:
- The loop is designed to run until
count
reaches 10. - However, when
count
equals 5, thebreak
statement is executed, causing the loop to terminate early.
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 on to the next iteration.
count = 0while count < 5: count += 1 if count == 3: continue print("Count is:", count)
Output:
Count is: 1Count is: 2Count is: 4Count is: 5
Explanation:
- When
count
equals 3, thecontinue
statement is executed, causing the loop to skip theprint
statement for that iteration.
Common Pitfalls
- Infinite Loops: Make sure the condition will eventually become false, or include a
break
statement to prevent the loop from running forever.
Key Points
- A
while
loop is used when you want to repeat an action as long as a condition is true. - Ensure that the loop condition will eventually become false to avoid infinite loops.
- Use
break
to exit a loop prematurely andcontinue
to skip an iteration.
Understanding the while
loop is essential for writing programs that need to repeat actions based on dynamic conditions, making it a fundamental concept in programming.
0 Comments
Please do note create link post in comment section