0

Why output is 17 17

int main() { int a=0,b=0; while (a<7, b<17) { a++; b++; } printf ("%d\n%d", a,b); return 0; }

19th Dec 2019, 10:46 AM
Somvir Dhaka
Somvir Dhaka - avatar
4 Antworten
+ 1
How output is 17
20th Dec 2019, 6:17 AM
Somvir Dhaka
Somvir Dhaka - avatar
+ 1
Thanks brother
20th Dec 2019, 9:39 AM
Somvir Dhaka
Somvir Dhaka - avatar
0
Comma operator between the two conditions `a < 7` and `b < 17` works by evaluating and then discarding its left-hand operand. So in your case, only `b < 17` is eventually used as loop condition. https://en.m.wikipedia.org/wiki/Comma_operator Hth, cmiiw
19th Dec 2019, 10:53 AM
Ipang
0
Did you even read the link in my previous post? it should explain how and why it happens. Initially, both <a> and <b> are zero. Then you setup a loop, the loop has two conditions `a < 7` and `b < 17`. Since you put Comma operator between the two `if` conditions, the left-hand operand `a < 7` be evaluated and then discarded (abandoned, ignored). Next the right-hand operand `b < 17` is evaluated, and this is the condition that decides when the loop should continue or stop. The loop should continue *while* <b> value is less than 17. Inside the loop body you increase value of <a> and <b>, and the instruction within loop body is repeated following the condition `b < 17` When the loop exits the value of <a> and <b> would be both 17. And that's how you have '17 17' in output.
20th Dec 2019, 6:47 AM
Ipang