Page 122 - computer science (868) class 11
P. 122

Operator          Meaning          Description          Usage            Example

                      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
                                                                            after the operand  b = a++;
                                                                                              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
                                                                                              (6-1=5)
                                                          It works the same   Postfix Unary
                                                          as val=val-1;     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


              Given below is the example of the prefix decrement operator. Here, the decrement 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: 4 and b: 4



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




                120120  Touchpad Computer Science-XI
   117   118   119   120   121   122   123   124   125   126   127