Python While Loop with Multiple Conditions

From the syntax of Python While Loop, we know that the condition we provide to while statement is a boolean expression.

This boolean expression could be a simple condition that compares two values or a compound statement containing multiple conditions.

To write simple condition, we can use Python Comparison Operators.

To write Python While Loop with multiple conditions, use logical operators like Python AND, Python OR to join single conditions and create a boolean expression with multiple conditions.

Example 1 – While Loop with Multiple Conditions joined by AND

In this example, we will write a while loop with condition containing two simple boolean conditions joined by and logical operator.

Python Program

i = 20
j = 15

while i > 0 and j > 0 :
    print((i,j))
    i -= 3
    j -= 2
Try Online

Output

(20, 15)
(17, 13)
(14, 11)
(11, 9)
(8, 7)
(5, 5)
(2, 3)
ADVERTISEMENT

Example 2 – While Loop with Multiple Conditions joined by OR

In this example, we will use Python OR logical operator to join simple conditions to form a compound condition to use for while loop condition.

Python Program

i = 20
j = 15

while i > 0 or j > 0 :
    print((i,j))
    i -= 3
    j -= 2
Try Online

Output

(20, 15)
(17, 13)
(14, 11)
(11, 9)
(8, 7)
(5, 5)
(2, 3)
(-1, 1)

Conclusion

Concluding this Python Tutorial, you can write a while loop condition with multiple simple conditions joined by logical operators.