Page 159 - ComputerScience_Class_11
P. 159

The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java
                   Options

                  Enter 1st number: 4
                  Enter 2nd number: 3
                  Sum: 7
                  Product: 12
                  Difference: 1
                  Quotient: 1.3333333333333333
                  Remainder: 1


                 The table given below shows the unary arithmetic operators which deal with one operand:
                             Operator          Meaning          Description         Usage            Example
                         Unary +           Unary Plus        It returns the same   Used before the   double val=45.76;
                                                             value.            operand.          then +val will be
                                                             The value of the                    +45.76 which is
                                                             operand remains                     same as 45.76.
                                                             the same.
                         Unary -           Unary Minus       It returns the value   Used before the   double val=-45.76;
                                                             with the reversed   operand.        then -val will be
                                                             sign.                               45.76 as -(-45.76) is
                                                                                                 45.76.
                         Unary ++          Unary Increment   It is used to     Prefix Unary      int a=5, b;
                                                             increase the value   Increment –    b = ++a;
                                                             of the operand by   Used before the   Ans. 6
                                                             1.                operand.          (5+1=6)


                                                             It works the same   Postfix Unary   int a=5, b;
                                                             as val=val+1;     Increment – Used   b = a++;
                                                                               after the operand.  Ans. 5
                         Unary --          Unary Decrement   It is used to     Prefix Unary      int a=6, b;
                                                             decrease the value   Decrement –    b = --a;
                                                             of the operand by   Used before the
                                                             1.                operand.          Ans. 5
                                                             It works the same                   (6-1=5)
                                                             as val=val-1;     Postfix Unary
                                                                               Decrement –       int a=6, b;
                                                                               Used after the    b = a --;
                                                                               operand.          Ans. 6
                 Let us understand the prefix and postfix increment and decrement operators with the help of some examples.

                 The prefix increment/decrement operator works on the principle of ‘Change Before Action’. Given below is the example
                 of the prefix increment operator. Here, an increment by 1 is executed first and then the value is used.


                                             int a=5, b;
                                             System.out.println("a: " +a);
                                             b=++a;
                                             System.out.println("a: " +a+ " and b: " +b);
                                             Output:
                                             a: 5
                                             a: 6 and b: 6




                                                                                            Variables and Expressions  157
   154   155   156   157   158   159   160   161   162   163   164