HOWTO · Python

Python while Loop With Multiple Conditions

This tutorial demonstrates how to use while loops with multiple conditions

On this page

The while loop in Python is a loop that keeps running the code as long as the condition in the while statement, i.e., the test condition, stays true; it stops as soon as the condition becomes false. This loop is used when the user doesn’t know the number of iterations to be carried out beforehand. There are many cases in which a while loop is used in multiple conditions.

In this tutorial, we will see how to use while loops with multiple conditions.

Use and and or Logical Operators to Make a Python while Loop With Multiple Conditions

The and logical operator requires both conditions to hold. It evaluates the conditions from left to right and stops as soon as one of them is false, because the combined result is already decided; only when the first condition is true does it go on to check the second. If any one of the two conditions is not satisfied or is not true, then the statement will be considered false, and the code won’t run.

Example:

x = 10
y = 20
initial_count = 0
while initial_count < x and initial_count < y:
    print(f"count: {initial_count}, x = {x}, y = {y}")
    initial_count += 1

Output:

count: 0, x = 10, y = 20
count: 1, x = 10, y = 20
count: 2, x = 10, y = 20
count: 3, x = 10, y = 20
count: 4, x = 10, y = 20
count: 5, x = 10, y = 20
count: 6, x = 10, y = 20
count: 7, x = 10, y = 20
count: 8, x = 10, y = 20
count: 9, x = 10, y = 20

In this method, we first make two variables, x, and y, equal to 10 and 20, respectively. Then we initialize the count equal to zero. After doing all this, we start with our while loop, which makes two conditions as shown in the above code. The loop keeps running only while both conditions are true at the same time. After ten iterations the count reaches 10, and at that point the first condition, initial_count < x, is false, so the loop ends even though the second condition, initial_count < y, would still hold. That is why the last printed line shows the count of 9.

Let’s move on to the or logical operator. The or operator also evaluates the conditions from left to right, but it stops as soon as one of them is true, because the combined result is already decided. The advantage of using this operator is that even if one of the two conditional statements is true, the code will execute. If both the statements turn out to be false, then the code will not run.

Example:

x = 10
y = 20
initial_count = 0
while initial_count < x or initial_count < y:
    print(f"count: {initial_count}, x = {x}, y = {y}")
    initial_count += 1

Output:

count: 0, x = 10, y = 20
count: 1, x = 10, y = 20
count: 2, x = 10, y = 20
count: 3, x = 10, y = 20
count: 4, x = 10, y = 20
count: 5, x = 10, y = 20
count: 6, x = 10, y = 20
count: 7, x = 10, y = 20
count: 8, x = 10, y = 20
count: 9, x = 10, y = 20
count: 10, x = 10, y = 20
count: 11, x = 10, y = 20
count: 12, x = 10, y = 20
count: 13, x = 10, y = 20
count: 14, x = 10, y = 20
count: 15, x = 10, y = 20
count: 16, x = 10, y = 20
count: 17, x = 10, y = 20
count: 18, x = 10, y = 20
count: 19, x = 10, y = 20

Note that in this method, the loop does not stop when the count reaches 10. The first condition becomes false at that point, but the second condition, initial_count < y, is still true, and one true condition is enough for or. The loop therefore continues until the count reaches 20, where both conditions are false, and the last printed line shows the count of 19.

Use not Logical Operators to Make a Python while Loop With Multiple Conditions

There is one more logical operator by which we can use the while loop in multiple conditions, and that operator is called the not operator. This operator is used to return the opposite value of the boolean statement. For example, if the boolean state is not False, then true will be returned, and if the boolean statement is not True, then false will be returned.

Example:

x = 10
y = 20
initial_count = 0
while not initial_count > x and not initial_count > y:
    print(f"count: {initial_count}, x = {x}, y = {y}")
    initial_count += 1

Output:

count: 0, x = 10, y = 20
count: 1, x = 10, y = 20
count: 2, x = 10, y = 20
count: 3, x = 10, y = 20
count: 4, x = 10, y = 20
count: 5, x = 10, y = 20
count: 6, x = 10, y = 20
count: 7, x = 10, y = 20
count: 8, x = 10, y = 20
count: 9, x = 10, y = 20
count: 10, x = 10, y = 20

Here, the while condition means the loop runs while the initial count is not greater than x and not greater than y. The and in the middle follows the same rule as before: because the first not statement is true for every count up to 10, the second not statement is evaluated as well. The loop stops at the count of 11, where not initial_count > x becomes false and and short-circuits without checking the second part. Since x is the smaller of the two limits, it is the one that ends the loop, and the last printed line shows the count of 10. Written differently, not A and not B is the same as not (A or B), so this loop runs while the count is greater than neither limit.

So, just like we have taken only two conditions in the while loop as an example, more of these statements can be added.