How while loop works with two conditions in c ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How while loop works with two conditions in c ??

while(a<7,b<17) { a++; b++; } printf("%d %d",a,b); the answer of this challenge is 17,17 don't know how 🤔🤔

28th Oct 2018, 9:21 AM
Randeep Singh
Randeep Singh - avatar
6 Answers
+ 9
I'd either go for a<7&&b<17 or a<7||b<17. &&: both conditions must be true, ||: it stops as soon as one of the conditions is true
28th Oct 2018, 10:01 AM
Anna
Anna - avatar
+ 3
The comma operator evaluates the first expression, throws the value away, and then evaluates the second expression. if (x = 0) will not execute, but if (x = 0, x >-1) will. You only use , when the first expression *does* something (has a side effect).
28th Oct 2018, 1:17 PM
Vlad Serbu
Vlad Serbu - avatar
+ 3
very thanks
28th Oct 2018, 10:54 PM
Ehsan Tajik
Ehsan Tajik - avatar
+ 2
I think I understand the question now. If the answer is 17, 17 the loop should be while((a<7) || (b<17)){ a++; b++; } This means the loop runs as long as either of the conditions are true. Since both are less than statements only the higher number would matter. Same thing could be accomplished with while(b<17){ a++; b++; } If && was used instead of || then the output would be 7 7.
2nd Nov 2018, 3:27 PM
Ryan
Ryan - avatar
+ 1
You could add two if statements to stop a at 7 and b at 17 int main(void){ int a, b = 0; while((a<7) || (b<17){ if(a < 7){ a++; } if(b < 17){ b++; } return 0; } }
31st Oct 2018, 9:20 PM
Ryan
Ryan - avatar
+ 1
While(a<7 || b<17) //that for "or" While(a<7 && b<17) //that for "and"
3rd Nov 2018, 9:12 AM
SnowAnna_
SnowAnna_ - avatar