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

About ++x

What I have learned in C++ is that ++x is used to bring the same number that's listed just before. But while playing with an opponent the answer will be in favour of the one who adds 1 to x. eg. x=3 then ++x should be 3 is what I remember. But those who bring 4 will won.

20th Jun 2020, 2:51 PM
Tony Abraham Varughese
Tony Abraham Varughese - avatar
8 Answers
+ 5
Tony Abraham Varughese There is no difference when used alone, the difference is when you use them in an expression. X++ evaluates x, then increments it (post-incrementation). ++x increments x, then evaluates it (pre-incrementation). Example: int a = 1; int b = a++; //b = 1, a = 2 System.out.println(b); //prints 1 a = 1; b = ++a; //b = 2, a = 2 System.out.println(b); //prints 2
20th Jun 2020, 3:01 PM
Arsenic
Arsenic - avatar
+ 3
Tony Abraham Varughese "++x" is same as "x=x+1". So when x=3 then ++x would be 3+1=4.
20th Jun 2020, 2:54 PM
Arsenic
Arsenic - avatar
+ 2
Arsenic Then there might not be any difference between ++x and x++
20th Jun 2020, 2:57 PM
Tony Abraham Varughese
Tony Abraham Varughese - avatar
+ 1
Arsenic Expression; that's where I've stucked. Thanks
20th Jun 2020, 3:04 PM
Tony Abraham Varughese
Tony Abraham Varughese - avatar
20th Jun 2020, 3:10 PM
Vasile Eftodii
Vasile Eftodii - avatar
0
It is a type of pre increment operation . So, when it is used in any operation first increase the value then, go for airthmetic operation. As for example; x=5,y=9; A=++x+y; So, in this firstly x increase its value by one and then will add to y. A=6+9=15
22nd Jun 2020, 3:24 AM
Kaushal Kishore
Kaushal Kishore - avatar
0
Kaushal Kishore In your example x=5. But, I couldn't understand the logic that gives ++x=10
22nd Jun 2020, 4:48 AM
Tony Abraham Varughese
Tony Abraham Varughese - avatar
0
Oo sorry
23rd Jun 2020, 1:35 AM
Kaushal Kishore
Kaushal Kishore - avatar