
while
Loop
While a for loop is associated with iterating over iterables, a while
loop relies on comparison operations. It continues executing as long as the comparison evaluates to True. It provides more flexibility as the number of iterations is not fixed and depends on when the condition evaluates to false. Let's explore some examples.
while
Loop Syntax:
while condition: # Code to be executed while the condition is true
- condition: often a comparison operation. The
while
loop continues as long as this condition evaluates to True, and stops once it turns False.
Example 1
# Assign 1 to a variable named "count"count = 1
# Use while loop to execute the code inside the loop a certain times until the count value becomes larger than 5while count <= 5: print("This is iteration", count) # add 1 to the count variable count += 1
Output:
This is iteration 1
This is iteration 2
This is iteration 3
This is iteration 4
This is iteration 5
Explanation
count = 1
: This line initializes the variablecount
with the value 1.
while count <= 5
: This is the start of awhile
loop. It sets up a condition that says, "While the value of the variable count is less than or equal to 5, do the following."print("This is iteration", count)
: Inside thewhile
loop, this line prints the current value of count. It will print the value of count during each iteration of the loop.count += 1
: After printing the value of count, this line increments the value of count by 1. This step is crucial because it ensures that the conditioncount <= 5
will eventually become False, enabling the loop to stop.
Example 2
Example below uses while
loop to take user input repeatedly until a specific condition is met.
# Assign an empty string to a variable named user_inputuser_input = ""
while user_input != 'quit': # Use input() function to ask for user input user_input = input("Enter a command (type 'quit' to exit): ") print(f"You entered: {user_input}")
Output:
Enter a command (type 'quit' to exit): test1
You entered: test1
Enter a command (type 'quit' to exit): test2
You entered: test2
Enter a command (type 'quit' to exit): quit
You entered: quit
Explanation
user_input = ""
: This line initializes a variable calleduser_input
and assigns it an empty string as its initial value. This variable will be used to store the user's input.while user_input != 'quit'
: This is the start of a while loop. It sets up a condition that says, "While the value of user_input is not equal to "quit", do the following."user_input = input("Enter a command (type 'quit' to exit): ")
: Inside the while loop, this line prompts the user to enter a command. It uses theinput()
function to read the user's input from the keyboard and assigns it to theuser_input
variable. The user's input will be stored as a string.print(f"You entered: {user_input}")
: After getting the user's input, this line prints a message that includes what the user entered.
The while loop keeps prompting for user input until "quit" is entered. As shown in the Output section, in the first two iterations, "test1" and "test2" were entered, and upon entering "quit" the third time, the loop ceased.