While Loop
While Loop Syntax
The syntax for a while loop in Python is as follows:
while condition: # loop body
The while loop will continue to execute as long as the condition is True. The condition is checked at the beginning of each iteration, so if the condition is False when the loop is first encountered, the loop body will never be executed.
Here is an example of a simple while loop that counts down from 5 and prints the countdown to the console:
count = 5 while count > 0: print(count) count -= 1 print("Liftoff!")
5 4 3 2 1 Liftoff!
Best Practices when Defining your Loop
- Make sure to include a condition in the while loop that will eventually become False, otherwise the loop will become infinite and will never terminate.
- Make sure to indent the loop body properly, as indentation is used to indicate the beginning and end of the loop body in Python.
- Test your while loop with different input to ensure that it is working as intended.
While Loop Characteristics
There are a few characteristics of while loops that you should be aware of:
- The while loop will continue to execute as long as the condition is True. If the condition is False when the loop is first encountered, the loop body will not be executed.
- The loop body must be indented. In Python, indentation is used to indicate the beginning and end of the loop body.
- The loop variable (in this case, count) must be updated inside the loop. If the loop variable is not updated, the loop will become infinite and will never terminate.
- The break statement can be used to exit a while loop prematurely.
- The continue statement can be used to skip the remainder of the current iteration and move on to the next one.
While Loop Manipulation
There are a few ways to manipulate a while loop:
Using the break Statement
The break statement can be used to exit a while loop prematurely. When the break statement is encountered, the loop is terminated and the program continues with the next statement after the loop.
Here is an example of using the break statement to exit a while loop:
count = 0 while True: # infinite loop count += 1 if count > 10: break # exit loop when count > 10 print(count) print("Done!")
This will output the following:
1 2 3 4 5 6 7 8 9 10 Done!
Using the continue Statement
The continue statement can be used to skip the remainder of the current iteration and move on to the next one. When the continue statement is encountered, the program continues with the next iteration of the loop.
Here is an example of using the continue statement to skip even numbers:
count = 0 while count < 10: count += 1 if count % 2 == 0: # skip even numbers continue print(count) print("Done!")
1 3 5 7 9 Done!
Best Practices
- Use the break statement sparingly, as it can make your code harder to read and understand. Only use it when necessary to exit a loop prematurely.
- Use the continue statement sparingly, as it can also make your code harder to read and understand. Only use it when necessary to skip the remainder of the current iteration and move on to the next one.