0
I don't understand how the output becomes 16. Can someone explain
6 Réponses
+ 2
Step-by-step explanation:
int a = 2;
int b = 0;
We declared two variables, you see.
cout << ++b;
Guess what it'll show...
1
But afterwards if we ask it again:
cout << b;
It will show...
1 again.
Why?
Because ++b means b = b + 1, not just b + 1; ++b changes the value of b whenever you use it.
When you say b = ++a it works so:
++a => a = a + 1 => a = 2 + 1 => a=3
b = a => b = 3
The same staff with c:
c = ++b => b was 3, and becomes 4, c = b => c = 4
Last string is 4*4 = 16;
Better check the theory of increment and decrement. There are some aspects you should know but that is for another topik.
+ 3
int a = 2 checked
int b = ++a // a=3 b=3 because a is a+1 and it becomes 3 before declaring b
int c = ++b// c=4 b=4 same thing at the above b was 3 and now it is b+1 before declaring c it is 4
We call it prefix you can read about it
+ 1
You increase b while increasing c
so the both of them become 4
+ 1
Thanks. I failed it in a challenge. Now i get it
0
Can you make comments on the code. I still don't understand
0
Thanks for explaining