+ 1

I++, i--.... And all the equivalents

An explanation please? I can't remember what they mean.

12th May 2019, 9:34 AM
BrandnameOra
BrandnameOra - avatar
8 odpowiedzi
+ 23
• Increment and Decrement Operators Like C and C++, Java provides increment and decrement operators. Specifically, it provides the plus-one increment (++) and decrement (−−) operators.  • If such an operator is used in front of a variable reference, then 1 is added to (or subtracted from) the variable and its value is read into the expression.  • If it is used after a variable reference, then the value is first read and then the variable is incremented or decremented by 1. So, for example, the code fragment     int i = 8;     int j = i++;     int k = ++i;     int m = i−−;     int n = 9 + i++; assigns 8 to j, 10 to k, 10 to m, 18 to n, and leaves i with value 10.  ➝ Remember to use  🔍SEARCH. . .  bar to avoid from posting duplicate threads!
12th May 2019, 10:27 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 23
BrandnameOra   [ ++i and i++ both add 1 to i ] However, ++i returns the new value, and i++ returns the old value. Likewise for decrement, --i and i--, which subtracts 1. • https://code.sololearn.com/chPAcDs7YZgV/?ref=app
12th May 2019, 2:04 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 20
BrandnameOra 😊Here is a great example with great explanation!👍 • https://code.sololearn.com/cKldmmXb8l8R/?ref=app
12th May 2019, 7:56 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
Try printing the values directly to console you will understand it straight away. Example //using 2 seperate variables x and y int x = 0; int y = 0; //Here we use the value of x first then increment afterwards System.out.print(x++); //output 0 //Here we increment the value of y first and then use it System.out.print(++y); //output 1
12th May 2019, 10:03 AM
D_Stark
D_Stark - avatar
+ 7
Using ++ is useful when you want to use a variable value and increment it by 1 same time. ++x if you want to increment x before using it. x++ if you want to use the original value of x before incrementing it.
12th May 2019, 9:45 AM
Seb TheS
Seb TheS - avatar
+ 6
> I can't remember what they mean. Just read the expression from the left to the right: in i++, i is used first and then incremented. In --i, i is first decremented and then its (new) value is used.
12th May 2019, 9:58 AM
Anna
Anna - avatar
+ 4
i++ is the same as incrementing i by 1 or i = i + 1 similarly i-- is the same as decrementing i by 1 or i = i-1 to increment or decrement by more than one you could use i+=2 or i-=2 respectively
12th May 2019, 9:38 AM
Neville
Neville - avatar
+ 2
Thank you all very much for your answers. I'm getting the hang of it now and it's good stuff to have gotten. Thank you again
12th May 2019, 1:52 PM
BrandnameOra
BrandnameOra - avatar