+ 1
How while() work without condition instead with increment?
how this code work and what is output? int a=2; int b=3; while(b--) ++a; cout<<a;
7 ответов
+ 5
while() works with a boolean conditionals
in this case, 'b' variable is the conditional, as 0 is considered false, and all other values are considered true
so 'b' is decremented each iteration until reaching 0, and then the conditional does not return true anymore, causing the loop to break
+ 3
without while?
and without cout?
+ 3
i just ran it as well in the c++ compiler in the code playground
got 5 as expected...
mind posting the full code including main and include?
+ 3
tnx now it works correctly...
+ 2
Burey covered the "how" pretty well, so I'll stick with the output:
int a=2; no output, a=2
int b=3; no output, b=3
while(b--) ++a; no output, b=3[-1 after], a=3
No output again, b=2[-1 after], a=4
No output again, b=1[-1 after], a=5
No output again, b=0 -- 0 == false, loop broken.
cout<<a; outputs: 5
0
so if i change a and b value like this
a=3
b=2
it return no output!!!
what is problem?
0
no
int a=3;
int b=2;
while(b--) ++a;
cout<<a;
i mean this code just change value