The while
loop in Python is used to repeatedly execute a block of code as long as a specified condition is true. It's particularly useful when the number of iterations is not known beforehand or when you want to repeatedly execute code until a certain condition is met.
Syntax:
while condition:
# Code block to execute as long as condition is true
statement1
statement2
...
# Code block to execute after the while loop completes (optional)
statement3
statement4
...
Explanation:
- The
while
keyword initiates the loop. condition
is an expression that evaluates to either True or False. If the condition is True, the code block inside the loop is executed; if False, the loop terminates, and control jumps to the next statement after the loop.- The code block inside the loop is executed repeatedly as long as the condition remains true.
- After the loop completes (when the condition becomes false), the code block outside the loop (if present) is executed.
Example: Counting Down
Let's use a while
loop to count down from 5 to 1:
count = 5
while count > 0:
print(count)
count -= 1
Explanation:
- In this example, the variable
count
is initialized to 5. - The
while
loop checks if the value ofcount
is greater than 0. Since it is, the loop enters and executes the code block inside the loop. - Inside the loop, the current value of
count
is printed, and thencount
is decremented by 1 (count -= 1
). - The loop continues to execute until the value of
count
becomes 0 (inclusive), at which point the conditioncount > 0
evaluates to False, and the loop terminates.
Example: Guessing Game
Here's an example of a simple guessing game where the user has to guess a secret number:
import random
secret_number = random.randint(1, 100)
guess = 0
while guess != secret_number:
guess = int(input("Guess the secret number (between 1 and 100): "))
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
print("Congratulations! You guessed the secret number:", secret_number)
Explanation:
- In this example,
random.randint(1, 100)
generates a random integer between 1 and 100, which is the secret number the user has to guess. - The
while
loop continues until the user's guess (guess
) matches the secret number (secret_number
). - Inside the loop, the user is prompted to input their guess using the
input()
method and the input string is converted to an integer usingint()
. - Depending on whether the guess is too low or too high compared to the secret number, appropriate feedback is provided.
- When the user correctly guesses the secret number, the loop terminates, and a congratulatory message is displayed.
Key Points:
- The
while
loop is useful when you need to repeat a block of code indefinitely or until a specific condition is met. - Be careful to avoid infinite loops, where the condition never becomes false, leading to the program running indefinitely.
- Ensure that the condition in the
while
loop eventually becomes false to prevent infinite looping.
An infinite loop occurs when the condition in a loop always evaluates to true, causing the loop to execute indefinitely. The most common way to create an infinite loop intentionally is by using the while True
construct, where True
is always true. Infinite loops are useful in situations where you want a program to continuously perform a task until explicitly stopped.
Example of Infinite Loop:
while True:
print("This is an infinite loop!")
Explanation:
- In this example, the condition
True
is always true, so thewhile True
loop continues to execute indefinitely. - Inside the loop, the statement
print("This is an infinite loop!")
is executed repeatedly, printing the same message over and over again.
Combining with input()
Method:
You can combine an infinite loop with the input()
method to create an interactive program that continues to execute until a specific condition is met.
Example: Interactive Menu
while True:
print("1. Option 1")
print("2. Option 2")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == "1":
print("You selected Option 1")
elif choice == "2":
print("You selected Option 2")
elif choice == "3":
print("Exiting the program...")
break # Exit the loop and terminate the program
else:
print("Invalid choice. Please try again.")
Explanation:
- In this example, the program displays a menu with options 1, 2, and 3.
- Inside the
while True
loop, the user is prompted to enter their choice using theinput()
method. - Depending on the user's choice, different actions are performed:
- If the user selects Option 1 or Option 2, a corresponding message is printed.
- If the user selects Option 3, the program prints a farewell message and exits the loop using the
break
statement, terminating the program. - If the user enters an invalid choice, a message is displayed, and the loop continues to prompt for input.
Key Points:
- Infinite loops are created using constructs like
while True
, where the loop condition is always true. - They are useful for tasks that need to continue indefinitely until explicitly stopped.
- Be cautious when using infinite loops to avoid situations where the program becomes unresponsive or consumes excessive resources. It's essential to have a mechanism to exit the loop when necessary.