Can anyone explain how it works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone explain how it works?

x=5,y=9; x=++x; y=--y; y-=x++; cout<<y;

3rd Jan 2020, 8:47 AM
Shuvam Chanda
Shuvam Chanda - avatar
8 Answers
3rd Jan 2020, 8:58 AM
InfinityAJ
InfinityAJ - avatar
+ 2
Let me simplify it for you. Then you may be able to understand X=5,y=9; x=x+1;//x=6 y=y-1;//y=8 y=y-x++//y=8-6 cause it is postfix it will return x's value first then increment x by 1. May be now you can understand. If not briefly explain the doubt.
3rd Jan 2020, 9:11 AM
InfinityAJ
InfinityAJ - avatar
+ 1
Ipang sure buddy...actually was a just bit confused ...so thought of posting that part only
4th Jan 2020, 2:33 AM
Shuvam Chanda
Shuvam Chanda - avatar
+ 1
Yaa Bro ;-) ++ => Is increment operator which is unary operator too, it's means it work on single operand and this can be use as prefix and suffix if it's on prefix then it increment first then do other things like assiging printing etc but in suffix it's do other thing then do increment ok . -- => Is decrement operator which is unary operator too, it's means it work on single operand and this can be use as prefix and suffix if it's on prefix then it decrement first then do other things like assiging printing etc but in suffix it's do other thing then do decrement ok So, output must be 2.
4th Jan 2020, 7:07 PM
Abhishek nirwan
Abhishek nirwan - avatar
0
Anant Jain bro...I have also read that topic....but still couldn't get that question....Can u explian deeply?
3rd Jan 2020, 9:04 AM
Shuvam Chanda
Shuvam Chanda - avatar
0
Try to run that snippet in Code Playground, you will get compile error first because the type for variable <x> and <y> is undefined. Once you add type definition for variable <x> and <y>, try to run that snippet again. This time you might have an output plus some warnings. "The operation on x may be undefined" and "The operation on y may be undefined". So what these warnings try to tell us? The warning tells us that we should not make an attempt to access and/or modify a data within a single point of execution sequence. This warning comes up because there are attempts to assign value (modify) variable <x> and <y> while the value to be assigned is read (accessed) from the same variable (<x> and <y> respectively).
3rd Jan 2020, 1:34 PM
Ipang
0
And please use relevant tags, at least specify the relevant language of the code (C++ as it seems). It helps to improve context clarity of your question, and *may* increase your chances for relevant, effective and quality answers 👍
3rd Jan 2020, 1:40 PM
Ipang
0
Anant Jain Thank you bro
4th Jan 2020, 2:33 AM
Shuvam Chanda
Shuvam Chanda - avatar