next up previous contents
Next: Conditionals Up: Expressions Previous: Loops   Contents

More operators

The last loop example showed an operator which was not yet introduced, namely ++. Its effect is to increment its operand, f in the example, by 1. Similarly, f- would decrease f by 1.

The increment and decrement operators can be used in a postfix notation, as in the example, or a prefix notation, as in -f. The difference between these notations is the value returned by the expression. The postfix notation returns the value of the variable before the modification; in prefix notation, the value of the expression the new value of the variable.

Given the fact that the value of a compound expression is the value of the last expression contained in the compound, f++ is identical to

{
  int g = f;
  f = f + 1;
  g;
}

and -f equals

{
  f = f - 1;
}

The last kind of operators to be introduced are the modifying assignment operators. For example, f = f + x can be written as f += x. The same is true for every other binary operator. The precedence of these operators is equal to that of the normal assignment (they are the `etc' in table 2.3).


next up previous contents
Next: Conditionals Up: Expressions Previous: Loops   Contents
Pieter J. Schoenmakers tiggr at gerbil.org