Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do while loops with multiple conditions

Tags:

People also ask

Can you have 2 conditions in a while loop?

As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.

Can a while loop have 3 conditions?

The above three conditons in while loop working as OR operator. If one of the three conditions meet, the while loop stops.

Can while loop have multiple conditions C++?

While Loops with multiple conditionsYou can chain together multiple conditions together with logical operators in C++ such as && (And operator) and || (Or operator). What you're really doing is creating a longer equation that will return either True or False, determining whether the loop executes or not.


I have a while loop in python

condition1=False condition1=False val = -1  while condition1==False and condition2==False and val==-1:     val,something1,something2 = getstuff()      if something1==10:         condition1 = True      if something2==20:         condition2 = True  ' ' 

I want to break out of the loop when all these conditions are true, the code above does not work

I originally had

while True:       if condition1==True and condition2==True and val!=-1:          break 

which works ok, is this the best way to do this?

Thanks