+ 2

I have 3 question about C++(continue)

int x=1; if(x>0) { x=2; cout<<++x; } cout<<x; The answer is 31. But I think it must be 32, can you explain? Thanks for answers.

5th Aug 2017, 10:01 AM
Yusuf
Yusuf - avatar
5 Answers
+ 8
In reply to OP: The answer should be 33 unless you missed some syntax. int x=1; if(x>0) // true { x=2; // x becomes 2 cout<<++x; // 2 incremented to 3, prints 3 } cout<<x; // prints 3.
5th Aug 2017, 10:22 AM
Hatsy Rei
Hatsy Rei - avatar
+ 8
@Joseph Perhaps it was because you posted three seperate threads with similar titles causing some people to believe some were duplicates. Don't mind the downvotes.
5th Aug 2017, 10:19 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
@Joseph Hardrock Just report anything you consider suspicious. They will be reviewed and evaluated. Nothing will go wrong.
5th Aug 2017, 10:37 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
I posted three separate threads, because they are not squeezing in a 1 post.
5th Aug 2017, 10:30 AM
Yusuf
Yusuf - avatar
+ 2
But the Sololearn is saying that the answer is 31. And are we counting the increments in cout? Because in some situations for example in return we are not counting the increments or decrements. I guess some incrementing process doesn't affect original memory of value. What are you thinking about this?By the way, should I report this question to SoloLearn? Thank you.
5th Aug 2017, 10:34 AM
Yusuf
Yusuf - avatar