Python while loop with multiple conditions [SOLVED]

Introduction to while loop in Python

A while loop is a control flow statement that enables code to be performed repeatedly depending on a specified Boolean condition in most computer programming languages. You may see the while loop as a repeated if statement. For instance, if we wish to request a user for a number between one to ten, but we don't know how often they could enter a more significant number, we keep asking "while the value is not between 1 and 10."

There are many loops used in programming, the most commonly used loops are for loop and while loop. The main difference between for and while loops is that, in for loop, we know precisely the number of iterations and in the while loop, the number of iterations is unknown.

Advertisement

The basic syntax of the while loop in python is given below:

while condition:
    operation

In the while loop, we specify a condition and the loop will run until the condition specified is come true.

Python while loop with multiple conditions [SOLVED]

 

A while loop can have one or more conditions. We will start implementing a while loop by giving just one condition, later on, we will see what a while loop looks like with multiple conditions.

 

Example 1: While loop with one condition

So let’s create a scenario. Suppose we are interested to print “GoLinuxCloud” five times. Well, you have the options, you can print this by just using the print function as shown below:

print('GoLinuxCloud')
print('GoLinuxCloud')
print('GoLinuxCloud')
print('GoLinuxCloud')
print('GoLinuxCloud')

Wow, you did it well, but wait. What if I say to print it a thousand times? This is where the looping concept in any programming language will help you out. Loops help us to implement the same thing in a code multiple times. To do so, we will use a while loop.

i = 1
while i <= 5:
    print('GoLinuxCloud')
    i = i + 1

As you can see in the code snippet above, we have declared a variable i and assigned it a value of 1. In the while loop, we have specified a condition that if the value of i becomes less than or equal to 5, print “GoLinuxCloud”, then we are incrementing the i variable by adding 1 to it every time the loop runs. now, when the loop runs five times, the value of i will become 5 and when it tries to run six times, the condition will now become false and the loop will end. The above code will print “GoLinuxCloud” five times.

Advertisement

Output:

GoLinuxCloud
GoLinuxCloud
GoLinuxCloud
GoLinuxCloud
GoLinuxCloud

 

Example 2: Nested while loop

Nested loops mean a loop inside a loop. We can create multiple loops inside a while loop, in this case, the while loop will be called a nested while loop. Let’s have an example of the nested loop to understand it better.

i = 1
while i <= 2:
    print("GoLinuxCloud")
    j = 1
    while j <= 1:
        print("is the best")
        j = j + 1
    i = i + 1

In the code snippet above, we have implemented a while loop inside a while loop. We have again specified a variable i with an initial value of 1 and the condition in the first while loop is that if the value of i is less than or equal to 2, go inside the loop and print “GoLinuxCloud”, inside the loop, we have initialized another variable j with initial value 1, then we have another loop where the condition is specified if j is less than or equal to 1, print “is the best”, than we are adding 1 to variable j and then to variable i. The loop will again go back and start from the beginning until the value of i becomes greater than 2.

Output:

GoLinuxCloud
is the best
GoLinuxCloud
is the best

 

Example 3: While loop with multiple conditions

A while loop can have multiple conditions. To specify multiple conditions in a while loop, we use logical operators like AND, OR, and NOT to give multiple conditions to a while loop. We will see each logical operator with an example. Let’s start with AND operator.

 

While Loop using AND operator

The AND operator is used to evaluate two expressions. In a programming context, the AND operator means to evaluate two expressions separately and then combine them as a whole. This operator is very interesting. The AND operator specifies if A and B both are true then go inside the loop otherwise the condition will become false and the loop will not be executed. Let’s see an example.

Advertisement
a = 3
b = 2
count = 0
while count < a and count < b:
    print("GoLinuxCloud is the best website ever!")
    count +=1

The demonstration of multiple conditions is explained in the code above. We have two variables, a, and b with values 3, and 2. The count variable is initialized as 0. Two conditions have been given to the while loop, when the count becomes equal to a, and b, the loop will end. After the first iteration, the value of the count will become 1, and in the second iteration, it will become 2, now, the second condition is false, so the loop will end here.

Output:

GoLinuxCloud is the best website ever!
GoLinuxCloud is the best website ever!

 

While loop using OR operator

The OR operator is also very interesting. With the OR operator, only one condition is enough to be true to run the loop. If both are true then it’s okay but when both conditions are false, the loop will end.

a = 3
b = 5
count = 0
while count < a or count < b:
    print("GoLinuxCloud is the best website ever!",', Count : ',count)
    count +=1

In this example, we use the OR operator, and the variables a and b have been initialized as 3, and 5. In the OR operator, if only one condition is true, the loop will run, and the loop will stop if both conditions become false. The count is 0 at the first iteration and the loop will run, then in the second iteration, the count will be 1 and it will go on until the value of the count become greater than or equal to b. In this case, the loop will stop.

Output:

GoLinuxCloud is the best website ever! , Count :  0
GoLinuxCloud is the best website ever! , Count :  1
GoLinuxCloud is the best website ever! , Count :  2
GoLinuxCloud is the best website ever! , Count :  3
GoLinuxCloud is the best website ever! , Count :  4

As we can see, the count becomes 4 which is less than b, in the next iteration, the count will be 5 which is equal to b, so the condition is false now and the loop will be terminated.

Advertisement

We have used two conditions in a while loop, you can use many conditions together, there is no limit but the concept will be the same as we discussed in this article. That’s all for today. See you soon.

Thank you!

 

Summary

Today, we studied about while loop. We have gone through all the basic concepts of a while loop, how it works and how multiple conditions can be used with a while loop. We have demonstrated the while loop with some very useful examples, we have used AND, and OR logical operators. You can use this concept to develop while loops with more complex multiple conditions.

 

Further Reading

Python 'while' with two conditions: "and" or "or" - Stack Overflow
The while statement

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X