Questions like x++ + ++x - --x | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Questions like x++ + ++x - --x

it is hard to figure out ! I know that x++ gives no effect but pre operator are hard

22nd Dec 2016, 5:25 AM
ShIvam Rawat
ShIvam Rawat - avatar
2 Answers
+ 5
1. x++ means first use the value of x and then increment the value of x by 1. 2. ++x means to first increment the value of x by one and then use it . 3. same goes with x-- and --x respectively but in this case the value would be decremented. Now, for ex:- if we take the value of x to be 5, we have to solve the code from left to right always and keep check of operator precedence, so , placing the value of x as 5, 0. x++ + ++x - --x 1. 5++ + ++x - --x, the value of x is 5 but it is incremented to become 6 2. 5 + ++6 - --x, the value of x is first incremented to become 7 3. 5 + 7 - --7, the value of x is first decremented to again become 6 4. 5 + 7 - 6 = 6 ( answer) compiler would take it as, 5. 5++ + ++6 - --7; ( x becomes 6 in second expression ++6because the original value 5 has been use in the first expression and then incremented) ( x becomes 7 in the third expression because in second expression it has been incremented and latter in the third it has been decrement to make it finally 6)
22nd Dec 2016, 5:44 AM
Nishant_Shrinetra
Nishant_Shrinetra - avatar
+ 3
i have changed the answer please read it again with a cool mind
22nd Dec 2016, 6:20 AM
Nishant_Shrinetra
Nishant_Shrinetra - avatar