Please help me understand the differences between ++x, x++, --x, and x--. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

Please help me understand the differences between ++x, x++, --x, and x--.

8th Jan 2017, 10:50 AM
Hitesh Paloud
Hitesh Paloud - avatar
15 Answers
+ 24
Pre-increment and pre-decrement operators increments or decrements the value of the object and returns a reference to the result. 1) prefix increment (pre-increment) ==> ++ expr 2) prefix decrement (pre-decrement) ==> -- expr Post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement. 1) postfix increment (post-increment) ==> expr ++ 2) postfix decrement (post-decrement) ==> expr --
29th Jan 2018, 8:27 PM
\__(° = °)__/
+ 13
++X is the prefix increment operator, this means that the value of x is first incremented and then used in the program. X++ is postfix increment operator, this means that the value of x is first used in the program and then incremented. --x is prefix decrement operator, this means that the value of x is first decremented and then used in the program. x-- is postfix decrement operator, this means that the value of x is first used in the program and then decremented. X++ and ++x are equivalent to x = x + 1 and x-- and --x are equivalent to x = x - 1
8th Jan 2017, 10:57 AM
Akash Apoorv Yadav
Akash Apoorv Yadav - avatar
+ 10
++x => first increase the variable and then use it x++ => first use the variable then use it for example: $x = 4 echo $x++; => output = 4 $x = 4 echo ++$x; => output = 5 The same with --x and x-- (Example was with PHP)
8th Jan 2017, 10:53 AM
Manuel Wiltz
Manuel Wiltz - avatar
+ 6
plz give me examples
8th Jan 2017, 11:01 AM
Hitesh Paloud
Hitesh Paloud - avatar
+ 2
*first use the variable then increase it
8th Jan 2017, 10:54 AM
Skayo
Skayo - avatar
+ 2
#include <iostream> using namespace std; int main() { int x=10; int y=10; int z=++x; int i=y++; cout<<z<<endl; cout<<i<<endl; return 0; }
8th Jan 2017, 12:46 PM
ASNM
ASNM - avatar
+ 1
#include <iostream> using namespace std; main() { int a = 21; int c ; // Value of a will not be increased before assignment. c = a++; cout << "Line 1 - Value of a++ is :" << c << endl ; // After expression value of a is increased cout << "Line 2 - Value of a is :" << a << endl ; // Value of a will be increased before assignment. c = ++a; cout << "Line 3 - Value of ++a is :" << c << endl ; return 0; } output:: Line 1 - Value of a++ is :21 Line 2 - Value of a is :22 Line 3 - Value of ++a is :23
8th Jan 2017, 11:09 AM
Akash Apoorv Yadav
Akash Apoorv Yadav - avatar
+ 1
z=11, i=10
24th Jan 2018, 1:36 PM
L.R.
L.R. - avatar
+ 1
i have one doubt what is the associativity of postfix? when two operats have the same precidence
11th Aug 2018, 1:23 PM
tamil malar
tamil malar - avatar
27th Sep 2019, 5:13 AM
Arjun Patil
Arjun Patil - avatar
+ 1
Good
27th Sep 2019, 5:13 AM
Arjun Patil
Arjun Patil - avatar
+ 1
Yh
21st May 2022, 10:17 AM
David Tamaratare Oghenebrume
David Tamaratare Oghenebrume - avatar
0
WHo can explain me this one??Why I need i ?Why 3=3/10 ,the number has 1 number, ? What happen whe 5=5/100 ? #include <iostream> using namespace std; int main() { int n,m,i=0; cout<<"Introduceti numarul n: "; cin>>n; m=n; while (n!=0) { n=n/10; i++; } cout<<"Numarul "<<m<<" are "<<i<<" cifre!"; }
29th May 2018, 10:34 PM
Mihai
0
(doubt time)😢 In the case of counting digits present in a given number , what will be code and explanation for it.....plzz help me out😊☺️
5th Jun 2018, 8:40 AM
kunal
0
This post is really useful and helpful to know more about the things which you have shared. I appreciate you for such a great amount of information. I assure this would be beneficial for many people. https://www.dumpscafe.com/Braindumps-SY0-501.html
19th May 2020, 11:13 AM
jeni smith