xxxxxxxxxx
// This should work the same for any language with prefix and postfix increment and decrement.
// Just change the keywords to suit your language.
let array = [0, 1];
// INCREMENT ++
let index = 0;
let prefix_increment = array[++index];
// returns index + 1, array[1] = 1
console.log(prefix_increment); // 1
index = 0;
let postfix_increment = array[index++];
// returns index, then adds 1, array[0] = 0
console.log(postfix_increment); // 0
console.log(index); // 1
// DECREMENT --
index = 1;
let prefix_decrement = array[--index];
// returns index - 1, array[0] = 0
console.log(prefix_decrement); // 0
index = 1;
let postfix_decrement = array[index--];
// returns index, then subtracts 1, array[1] = 1
console.log(postfix_decrement); // 1
console.log(index); // 0
xxxxxxxxxx
++x : x is now 2
++x + : 2 +
++x + x : 2 + 2
++x + x++ : 2 + 2 and x is now 3
++x + x++ * : 2 + 2 *
++x + x++ * x : 2 + 2 * 3
xxxxxxxxxx
class Operator {
public static void main(String[] args) {
int var1 = 5, var2 = 5;
// 5 is displayed
// Then, var1 is increased to 6.
System.out.println(var1++);
System.out.println(var1); // 6 is displayed
// var2 is increased to 6
// Then, var2 is displayed
System.out.println(++var2);
}
}
xxxxxxxxxx
++x : x is now 2
++x + : 2 +
++x + x : 2 + 2
++x + x++ : 2 + 2 and x is now 3
++x + x++ * : 2 + 2 *
++x + x++ * x : 2 + 2 * 3
xxxxxxxxxx
++x = x is now 2
x++ = x is now 3 but the showed value remains 2
x = is 3
x = 2 + (2 * 3)
x = 8