+ 5

How can I run multiple conditions in an while loop?

12th May 2017, 9:53 AM
Yash Katyayan
Yash Katyayan - avatar
21 Antworten
+ 20
by use of the logical operators
13th May 2017, 2:53 AM
NimWing Yuan
NimWing Yuan - avatar
+ 17
Or even use the 'or' operator ( || ) ;P while ( (x>42) || (y<42) || (n==42) ) { /* ... */ }
12th May 2017, 1:59 PM
visph
visph - avatar
+ 9
since a while loop only runs if its condition is true, you can connect multiple conditions with the Boolean and (&&) while(condition1 && condition2 && condition3) { // do something }
12th May 2017, 10:16 AM
Mario L.
Mario L. - avatar
+ 7
Even you can use both. while(((5 > y) || (x < 5)) && (z != 10)) { /* my statements... */ }
12th May 2017, 2:06 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 7
You can use logical operators (&&, ||, !) or even the conditional operators (?:)
13th May 2017, 3:07 AM
Nihar Raote
Nihar Raote - avatar
+ 7
It's pretty much been answered albeit without much explanation: || if you want either condition being true to keep it running, && if you want either condition being false to end it. I think you need an explanation, too... With OR (||), we say loop if ANY of the following are true. With AND (&&), we're saying EVERY one of these must be true. I would like to add that if one condition or all conditions are false for && and || respectively, all code in your while block will not run. This could also be where you're going wrong (no code posted). If this is the case, a do-loop is better. Syntax: do { /* your code */ } while (condition) || (condition); Hopefully this better clarifies things.
21st May 2017, 9:45 PM
Jamie
Jamie - avatar
+ 5
Use or ||
18th May 2017, 10:53 AM
Iwan
Iwan - avatar
+ 5
@Jamie, I agree with your solution but I myself made a lot of use in my past programming profession that included the CASE structure in it to execute the DO WHILE loop. To exit the WHILE LOOP, I used a simpel FOR ELSE condition to change the WHILE condition to exit the LOOP. It is nice to see that some of you go the extra step to help a fellow programmer...
22nd May 2017, 10:18 AM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 3
And yet another method: You can combine if/case statements within your loop with the 'continue' command to add flow changes within a loop.
12th May 2017, 6:43 PM
Jim
Jim - avatar
+ 2
Suppose in a while loop, you have two conditions, and any one needs to be true to proceed to the body, then in that case you can use the || operator between those two conditions, and in case you want both to be true, you can use && operator.
13th May 2017, 3:18 PM
Himansh Mulchandani
Himansh Mulchandani - avatar
+ 1
By using if statements and logical operators such as &&, 11,!
13th May 2017, 10:03 AM
Ikram
Ikram - avatar
+ 1
using && or ||
19th May 2017, 7:35 PM
Akhilesh
Akhilesh - avatar
+ 1
using logical operators ✌
20th May 2017, 10:24 AM
Abhi Agarwal
Abhi Agarwal - avatar
+ 1
&& and ll
20th May 2017, 2:12 PM
Arkadiusz Witek
Arkadiusz Witek - avatar
+ 1
link then using Boolean operators
21st May 2017, 1:41 AM
Daniel Akpan
Daniel Akpan - avatar
0
use or function while ( (a>10) || (b<10) || (m==10) ) { /* have a fun */ }
14th May 2017, 9:10 AM
Siddharthraj raval
Siddharthraj raval - avatar
0
You can use logical operators i.e. or,and,not to combine these conditions to get the required outputs.
23rd May 2017, 3:25 AM
Sai Saketh Aluru
Sai Saketh Aluru - avatar
0
Yes
2nd Jul 2017, 7:08 AM
Bruh
- 1
while(condition) { while(condition) { while(condition) { } } }
18th May 2017, 3:57 PM
Lemahc Industries
Lemahc Industries - avatar
- 1
f*********************************k
14th Sep 2018, 10:17 AM
muthaiah