Last updated on December 28th, 2023 at 09:56 am
While Loop In Python:
A while loop in Python is used to execute a block of code repeatedly until a certain condition is met. Here is the basic syntax of a while loop:
while condition:
# code to be executed
The condition is checked at the beginning of each iteration. If it is True, the code inside the loop is executed. Afterward, the condition is checked again, and the loop continues until the condition becomes False.
Here is an example of a while loop that prints the numbers from 1 to 5:
count = 1
while count <= 5:
print(count)
count += 1
In this example, the count variable is initialized to 1. The condition count <= 5 is checked at the beginning of each iteration. If it is True, the current value of count is printed to the console and count is incremented by 1. After 5 iterations, the value of count is 6, which is not less than or equal to 5, so the loop ends.
You can also use the break statement inside a while loop to terminate the loop prematurely. For example, the following code uses a while loop to repeatedly ask the user for input until they enter a non-empty string or type “quit”:
while True:
user_input = input("Enter something (or type 'quit' to exit): ")
if user_input == "":
print("You didn't enter anything!")
elif user_input == "quit":
break
else:
print("You entered:", user_input)
In this example, the while loop runs indefinitely (True is always True). However, the break statement is used to exit the loop when the user types “quit”.
You can use the continue statement to skip the rest of the current iteration and move on to the next one. For example, the following code uses a while loop to print the numbers from 1 to 10, skipping the even numbers:
count = 1
while count <= 10:
if count % 2 == 0:
count += 1
continue
print(count)
count += 1
In this example, the if statement checks if the current value of count is even. If it is, the continue statement is executed, which skips the rest of the current iteration (including the print statement) and moves on to the next iteration.
- You can use the
elseclause to specify a block of code that should be executed when the loop terminates normally (i.e., when the condition becomesFalse). For example, the following code uses awhileloop to find the smallest power of 2 that is greater than or equal to a given number:
num = 20
power = 1
while power < num:
power *= 2
else:
print("The smallest power of 2 that is greater than or equal to", num, "is", power)
In this example, the while loop multiplies power by 2 on each iteration until it is greater than or equal to num. When the loop terminates (because the condition power < num is no longer True), the else block is executed, which prints a message to the console.
- You can use the
while-elseconstruct to combine thewhileloop andelseclause into a single statement. Theelseblock is executed when the condition becomesFalse, just like in the previous example. However, if you use thebreakstatement to exit the loop prematurely, theelseblock will not be executed. For example, the following code uses awhile-elseloop to print the numbers from 1 to 5:
count = 1
while count <= 5:
print(count)
count += 1
else:
print("Finished!")
In this example, the while loop prints the numbers from 1 to 5 and then terminates normally (because the condition count <= 5 is no longer True). The else block is then executed, which prints a message to the console.