While loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

While loop

Can we add two conditions in a single while loop using and operator.

26th Dec 2021, 12:19 PM
Debashish Das
Debashish Das - avatar
5 Answers
+ 2
Yes, absolutely. Example: while 1==1 and 2==2: print("Hello World!")
26th Dec 2021, 12:26 PM
skaltecho
+ 2
Well what if we take counter as condition. Like While i<=100 and i %2 == 0
26th Dec 2021, 12:29 PM
Debashish Das
Debashish Das - avatar
+ 2
Debashish Das your example makes maybe less sence. Yes, you can do that. But this loop will only run for even numbers. And maybe only once. It depends on what you are doing with i inside the loop. Maybe it would be better to put a if statement inside the while loop: while i <= 100: if i % 2 == 0: .... some code else: .... some code
26th Dec 2021, 12:46 PM
Coding Cat
Coding Cat - avatar
+ 1
no = [ "n","no"] yes =["y","yes"] con ="" while (cont not in no) and (cont in yes): con = input("Do you want to continue:")
27th Dec 2021, 6:10 PM
Saad Khan
Saad Khan - avatar
0
It can work but why do you want the second condition?
26th Dec 2021, 12:39 PM
Paul
Paul - avatar