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; }
4 ответов
+ 1
How output is 17
+ 1
Thanks brother
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
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.