While Loop
A while loop in Python repeatedly executes a block of code as long as a given condition is true. It's useful when you don't know the exact number of iterations beforehand.
Syntax
Python
while condition:
# code to be executed
How it works
1. The while keyword initiates the loop.
2. The condition is evaluated. If it's True, the code block inside the loop is executed.
3. The condition is checked again, and the process repeats until the condition becomes False.
Example
Python
count = 0
while count < 5:
print(count)
count += 1
This will output:
0
1
2
3
4
Important Points
- The condition is checked at the beginning of each iteration.
- Indentation is crucial to define the loop body.
- Be careful to avoid infinite loops by ensuring the condition eventually becomes False.
- You can use break to exit the loop prematurely and continue to skip the current iteration.
Example with break and continue
Python
number = 0
while number < 10:
number += 1
if number == 5:
break
if number % 2 == 0:
continue
print(number)
Key Differences Between for and while Loops
- for loops are typically used when you know the number of iterations beforehand.
- while loops are used when the number of iterations is unknown or depends on a condition.
While Loop with Else
The else clause in a Python while loop is executed only if the loop completes normally, without encountering a break statement. It's a way to specify code that should run after the loop has finished successfully.
Syntax
Python
while condition:
# code to be executed while the condition is True
else:
# code to be executed if the loop completes normally
How it works
1. The while loop continues to execute as long as the condition is True.
2. If the loop completes all iterations without encountering a break statement, the else block is executed.
3. If a break statement is encountered, the else block is skipped.
Example
Python
num = 23
flag = False
while num > 0:
if num % 2 == 0:
flag = True
print(num, "is even")
break
num -= 1
if flag:
print("At least one even number found")
else:
print("No even numbers found")
In this example, the else block will be executed only if the while loop completes without finding an even number.
Use Cases
- Executing code after a successful loop completion.
- Handling cases where a loop might not find a specific condition.
- Improving code readability and maintainability.
Important Notes
- The else block is not executed if the loop is terminated by a break statement.
- The else block can be used in combination with other loop control statements like continue.
Break and Continue in While Loops
Break Statement
The break statement is used to terminate a loop prematurely. When encountered, the program's control flow jumps to the statement immediately after the loop.
Python
count = 0
while count < 5:
print(count)
if count == 3:
break
count += 1
This code will output:
0
1
2
3
Continue Statement
The continue statement skips the rest of the current iteration of a loop and jumps to the beginning of the next iteration.
Python
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
This code will output:
1
2
4
5
Key Points
- Both break and continue are used within the loop body.
- break completely exits the loop.
- continue skips the current iteration and moves to the next.
- Use these statements judiciously to control the flow of your loop effectively.
By understanding and using break and continue appropriately, you can write more efficient and flexible Python code.