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

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: 5


                 Given below is an example of the postfix decrement operator. Here, the value is used first and then a decrement by
                 1 is done on the given value.


                                             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: 5


                 Some more examples are as follows:
                    1.  If int n1 = 5, n2 = 6, s;
                       s = n1++ + ++n2;
                  Ans.  12                    Working: 5 + 7 = 12
                    2.  If int a = 50, c;
                       c = a++ + ++a / 4;
                  Ans.  63                    Working: 50 + 52 / 4 = 50 + 13 = 63
                    3.  If int m = 12;
                       m = m++ / ++m * m;

                  Ans.  0                     Working: 12 / 14 * 14 = 0 * 14 = 0
                    4.  If int k = 10, l = 20, m;
                       m = ++k + --l;
                  Ans.  30                    Working: 11 + 19 = 30
                    5.  If int p = 5, k;
                       k = (++p * (p-- * 2));
                  Ans.  72                    Working: 6 * (6 * 2) = 6 * (12) = 72

                 Assignment Operator
                 The assignment operator is used to assign a value or an expression to a variable. Java has one assignment operator
                 which is “=” operator. For example:
                 int a = 50;
                 double d = 34.7;

                 Shorthand Operators
                 Java allows a way to represent some binary operators in a shorter way using shorthand assignment operators. They
                 assign an expression to a variable. They are also known as compound assignment operators.




                                                                                                                       121
                                                                                               Variables and Expressions  121
   118   119   120   121   122   123   124   125   126   127   128