fbpx

While Loops

While loops allow us to perform repetitive tasks more efficiently, by specifying a certain Boolean expression that controls how many times a given body of code will execute. Remember that Boolean expressions will always be either True or False, so as long as the expression is true, the segment of code under a while loop will execute, and when it is false, it will move on past the loop to the rest of the code. Here is what this looks like in code:

				
					while condition:
    # Code block to be executed
				
			

The condition is the Boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code block is executed, and then the condition is checked again. If the condition is still true, the code block is executed again, and this process continues until the condition becomes false. Once the condition becomes false, the control exits the while loop, and the program continues with the next statement after the loop.

Here’s an example to demonstrate the usage of a while loop:

				
					count = 0
while count < 5:
    print("Count:", count)
    count += 1

print("Loop finished")

# Output:
# 0
# 1
# 2
# 3
# 4
# Loop finished
				
			

In this example, the while loop continues to execute the code block as long as the count variable is less than 5. Inside the loop, the value of count is printed, and then it is incremented by 1 using the += operator. The loop iterates five times, printing the values 0, 1, 2, 3, and 4. Once the value of count becomes 5, the condition count < 5 becomes false, and the control exits the while loop. The program then continues with the next statement after the loop, which prints “Loop finished”.

As seen in the code above, essentially anything we can do with a for-loop could also be done with a while-loop. However, while loops are more preferable to use when the number of iterations is not fixed in advance, or we don’t know exactly how many times we want to execute the block of code (if we did, it would probably be more intuitive to just use the range() function). For instance, if I wanted to prompt the user to enter as many items to their grocery list as they like, and type “END” to finish, I could use a while loop like so:

				
					grocery_list = []

item = input("Enter a grocery item: ")
while item != "END":
    grocery_list.append(item)
    item = input("Enter another item: ")
    
print(grocery_list)
				
			

Notice in the above scenario that we don’t know how many items the user will enter, so we would consider this to be an unfixed number of iterations, which is why it would be preferable to use a while loop.

It’s important to ensure that the condition in a while loop eventually becomes false; otherwise, the loop will continue indefinitely, resulting in an infinite loop. To prevent infinite loops, you can include statements inside the loop that modify the condition or use a break statement to explicitly exit the loop when a certain condition is met.

Replit Practice

Loading...