Search

Friday, December 21, 2018

Assignment Operators, Increment and Decrements Operators

Java provides several assignment operators for abbreviating assignment expressions. 
For example, you can abbreviate the statement

c = c + 3; 
with the addition assignment operator, +=, 
as
c += 3;
The += operator adds the value of the expression on the right of the operator to the value of the variable on the left of the operator and stores the result in the variable on the left of the operator. Any statement of the form

variable = variable operator expression;

where operator is one of the binary operators +, -, *, / or % (or others we discuss later in the text), can be written in the form 

variable operator= expression;

Thus, the assignment expression c += 3 adds 3 to c. Figure shows the arithmetic assignment operators, sample expressions using the operators and explanations of what the operators do.


Assignment/operator   Sample expression    Explanation      Assigns                       

Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; 

+=                   c += 7               c = c + 7       10 to c
-=                   d -= 4               d = d - 4       1  to d
*=                   e *= 5               e = e * 5       20 to e
/=                   f /= 3               f = f / 3       2  to f
%=                   g %= 9               g = g % 9       3  to g


Increment and Decrements Operators 

Java provides the unary increment operator, ++, and the unary decrements operator, --, which are summarized in Figure. A program can increment the value of a variable called c by 1 using the increment operator, ++, rather than the expression c=c+1 or c += 1. If an increment or decrement operator is placed before a variable, it is referred to as the preincrement or predecrement operator, respectively. If an increment or decrement operator is placed after a variable, it is referred to as the postincrement or postdecrement operator, respectively. 

Operator         Called                 Sample expression           Explanation                              

++                   preincrement              ++a                              Increment a by 1, then use the                                                                                                  new value of a in the expression                                                                                                in which a resides.                       
++                   postincrement             a++                             Use the current value of a in the                                                                                                expression in which a resides,                                                                                                  then increment a by 1.                
--                     predecrement               --b                             Decrement b by 1, then use the                                                                                                new value of b in the expression                                                                                                in which b resides.                       
 --                    postdecrement               b--                           Use the current value of b in the                                                                                                expression in which b resides,                                                                                                  then decrement b by 1.               

Preincrementing (predecrementing) a variable causes the variable to be incremented (decremented) by 1, and then the new value of the variable is used in the expression in which it appears. Postincrementing (postdecrementing) the variable causes the current value of the variable to be used in the expression in which it appears, and then the variable value is incremented (decremented) by 1. The application in Figure demonstrates the difference between the preincrementing version and the postincrementing version of the ++ increment operator. Postincrementing the variable c causes it to be incremented after it is used in the System.out.println method call (line 13). Preincrementing the variable c causes it to be incremented before it is used in the System.out.println method call (line 20). The program displays the value of c before and after the ++ operator is used. The decrement operator (--) works similarly. 

// Increment.java 
// Preincrementing and postincrementing
public class Increment 
{
// main method begins execution of Java application
public static void main( String args[] ) 
{
int c;
c = 5;
System.out.println( c );   // print 5     
System.out.println( c++ ); // print then 5 postincrement
System.out.println( c );   // print 
System.out.println();      // skip a lin
System.out.println( c );   // print 5 
System.out.println( ++c ); // preincrement then print 6
System.out.println( c );   // print 6 
// end method main 
} // end class Increment

OUTPUT'S
5
6
6

Line 16,
System.out.println();      // skip a line
uses System.out.println to output a blank line. If println receives no arguments, it simply outputs a newline character. The arithmetic assignment operators and the increment and decrement operators can be used to simplify program statements. For example, the three assignment statements in  

(lines 32, 34 and 36),

passes = passes + 1; 
failures = failures + 1; 
student = student + 1;

can be written more concisely with assignment operators as

passes += 1; 
failures += 1; 
student += 1;

with preincrement operators as

++passes; 
++failures; 
++student;

or with postincrement operators as

passes++; 
failures++; 
student++;

It is important to note here that when incrementing or decrementing a variable in a statement by itself, the preincrement and postincrement forms have the same effect, and the predecrement and postdecrement forms have the same effect. It is only when a variable appears in the context of a larger expression that preincrementing and post-incrementing the variable have different effects (and similarly for predecrementing and postdecrementing). 

Common Programming Error                                                                                   
Attempting to use the increment or decrement operator on an expression other than an lvalue is a syntax error. An lvalue is a variable or expression that can  appear on the left side of an assignment operation. For example, writing ++(x + 1) is a syntax error, because (x + 1) is not an lvalue.  

The chart in Fig. 4.15 shows the precedence and associativity of the operators that have been introduced up to this point. The operators are shown from top to bottom in decreasing order of precedence. The second column describes the associativity of the operators at each level of precedence. Notice that the conditional operator (?:), the unary operators increment (++), decrements (--), plus (+), minus (-) and casts and the assignment operators =, +=, -=, *=, /= and %= associate from right to left. All other operators in the operator precedence chart in Fig. 4.15 associate from left to right. The third column names the groups of operators. 

Operators                           Associativity                                   Type                                   



()                                         left to right                                      parentheses                       ++ --                                    right to left                                      unary postfix                     ++ -- +  - (type)                   right to left                                      unary                                  *  /  %                                  left to right                                      multiplicative                     +  -                                      left to right                                      additive                               <  <= >  >=                          left to right                                      relational                            == !=                                   left to right                                      equality                              ?:                                        right to left                                      conditional                         =  += -= *= /= %=                right to left                                      assignment                        



0 comments:

Post a Comment