
The second column describes the associativity of the operators at each level of precedence. The operators are shown from top to bottom in decreasing order of precedence. For example, writing ++(x + 1) is a syntax error because (x + 1) is not a variable.įigure 4.17 shows the precedence and associativity of the operators we have introduced to this point. It is only when a variable appears in the context of a larger expression that preincrementing and postincrementing the variable have different effects (and similarly for predecrementing and postdecrementing).Īttempting to use the increment or decrement operator on an expression other than one to which a value can be assigned is a syntax error. When incrementing or decrementing a variable in a statement by itself, the prefix increment and postfix increment forms have the same effect, and the prefix decrement and postfix decrement forms have the same effect. Passes += 1 failures += 1 studentCounter += 1 Passes = passes + 1 failures = failures + 1 Ĭan be written more concisely with compound assignment operators as For example, the three assignment statements in Fig. The arithmetic compound assignment operators and the increment and decrement operators can be used to simplify program statements. Line 22 outputs c's value again to show that the value of c is still 6 after line 21 executes. This expression preincrements c, so its value is incremented, then the new value ( 6) is output. Line 21 outputs the value of the expression ++c. Line 19 resets c's value to 5, and line 20 outputs c's value. Line 14 outputs c's new value ( 6) to prove that the variable's value was indeed incremented in line 13.

Thus, line 13 outputs c's initial value ( 5) again. This expression postincrements the variable c, so c's original value ( 5) is output, then c's value is incremented. Line 13 outputs the value of the expression c++.

Line 11 initializes the variable c to 5, and line 12 outputs c's initial value. Preincrementing and postincrementing.Ħ public static void main( String args )ġ0 // demonstrate postfix increment operatorġ8 // demonstrate prefix increment operator
