Operators/Incrementation
From FizzFuzz
- Go back to Operators.
The incrementation operator is used to increment a numeric variable. That is, it adds one to the variable, and stores this in memory. The operator is unary and can either be prefix or postfix, with the operation changing depending on its position. The operator itself is ++ (two consecutive plus signs).
Contents |
Prefix
Notation
++ α
Where α is an int or float.
Operation
Prefix notation first increments the value of the operand, and then passes the value to the expression it's used in. This means that the value of α + 1 is used by the expression, and not just α. That is, the two following snippets are functionally equivalent:
int x = 1 x += 1 int y = x + 10;
int x = 1 int y = (++ x) + 10;
Example
int x = 0; output << (++ x); // Outputs "1". output << (++ x) + 2; // Outputs "4". output << x; // Outputs "2".
Postfix
Notation
α ++
Where α is an int or float.
Operation
Postfix notation first passes the value of the operand to the expression it's being used in, and then increments the value. This means the value of α is used by the expression, and not α + 1, as with prefix notation. The two following snippets are functionally equivalent.
int x = 1; int y = x + 10; x += 1;
int x = 1; int y = (x ++) + 10;
Example
int x = 0; output << (x ++); // Outputs "0". output << (x ++) + 2; // Outputs "3". output << x; // Outputs "2".

