I tried googling this but Google doesn't handle "--n" well. I saw this in my professor's code:
f[--n];
f[n++];
where f is an array of double values.
My guess is that it returns the value of f[n] before reducing (or adding) to n.
f[--n];
means :
n = n -1;
f[n];
f[n++];
means :
f[n];
n = n + 1;
It's actually a type of operator called a pre-decrement, and it's part of a family of 4 operators (see table of java operators)
For an integer-type variable called n:
post-increment n++
is the equivalent of n = n + 1
, the 'post' part means that if you see it in a line of code (ex. foo(n++);
) then the line of code will be called Before n is incremented.
pre-increment ++n
is also the same as n = n + 1
but it occurs Before the line of code it belongs in has been run.
post-decrement n--
is the equivalent of n = n - 1
and occurs After the current line of code has been run
pre-decrement --n
is the equivalent of n = n - 1
and occurs Before the current line of code has been run
Example of post vs pre decrement:
int n = 5;
System.out.println(n--); //This prints 5
System.out.println(n); //This prints 4
System.out.println(--n); //This prints 3
System.out.println(n); //this prints 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With