Loops are an essential concept in programming, allowing us to execute a block of code multiple times. One type of loop is the while
loop, which continues to run until a certain condition is met. In this article, we will introduce you to while
loops and show you how to use them with conditional tests. We will also provide examples and exercises to help you understand the concept better.
What is a while loop?
A while
loop is a control flow statement that allows us to repeat a block of code as long as a specified condition is true. The syntax for a while
loop in Python looks like this:
while condition:
# code block to be executed repeatedly
The condition
can be any expression evaluating to True
or False
. As long as the condition
evaluates to True
, the code block will keep executing. Once the condition becomes False
, the loop stops.
Using conditional tests with while loops
We can use conditional tests to control the execution of a while
loop. A conditional test is an expression that evaluates to either True
or False
. If the expression is True
, the code block associated with the loop executes. If it's False
, the loop exits.
Here's an example of how we can use a conditional test with a while
loop to count from 1 to 5:
count = 1
while count <= 5:
print(count)
count += 1
In this example, the condition
is count <= 5
. As long as count
is less than or equal to 5, the loop will continue to execute. Inside the loop, we print the current value of count
and then increment it by 1. When count
becomes 6, the condition becomes False
, and the loop exits.
Examples
Let's take a look at some more examples to help you understand how while
loops work with conditional tests.
Example 1 - Printing even numbers
In this example, we use a while
loop to print all even numbers from 0 to 10:
num = 0
while num <= 10:
print(num)
num += 2
The condition
is num <= 10
, which means the loop will execute as long as num
is less than or equal to 10. Inside the loop, we print the current value of num
and then increment it by 2. This ensures that we only print even numbers.
Example 2 - User input validation
We can also use while
loops to validate user input. In this example, we ask the user to enter a number between 1 and 10. If the user enters an invalid number, we keep asking until they provide a valid input:
num = 0
while num < 1 or num > 10:
num = int(input("Enter a number between 1 and 10: "))
The condition
is num < 1 or num > 10
, which means the loop will execute as long as num
is less than 1 or greater than 10. Inside the loop, we ask the user to enter a number between 1 and 10 using the input()
function. We then convert the input to an integer using the int()
function and assign it to the variable num
. The loop keeps executing as long as the user provides an invalid input.
Exercises
Now that you have a basic understanding of while
loops and conditional tests, it's time to put your knowledge into practice. Here are some exercises for you to try:
-
Write a program that asks the user to enter a password. Keep asking until they enter the correct password (which is "password123").
-
Write a program that prints the first 10 Fibonacci numbers. The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones.
-
Write a program that asks the user to enter a number and then prints all the factors of that number.
Conclusion
In this article, we introduced you to while
loops and showed you how to use them with conditional tests. We also provided examples and exercises to help you understand the concept better. Looping is an important concept in programming, and while
loops are just one way to implement it. With practice, you'll be able to write complex programs using loops with ease.