Page 64 - CA_Blue( J )_Class10
P. 64

After the calculation of the expression, the value of b = 4 and r = 20.


                                               b           *b        r=result      b--
                                               5           20          20           4

                                                                                b = 4
                                             After the execution of the program
                                                                                r = 20


                Here, the value of b = 5 is kept in working memory, and then it is decreased by 1. So it becomes 4. that, that it is
                 multiplied by the value in the working memory. The result becomes 20.
                Let us see some more examples:
              1.  If int a = 10, b;                                               [shows the difference prefix and postfix]
                b = ++a + a;
                   = 11+11
                   = 22
                b = a++ + a;
                   = 10+11
                   = 21
                b = a + a++
                   = 10+10
                   = 20
                b = a + ++a
                   = 10+11
                   = 21
              2.  If int a = 100, b = 10, c;                                [if datatype is not provided, then default is int]
                c = a++ + ++a / b
                   = 100 + 102/10                                                            [since int/int will return int]
                   = 100 + 10
                   = 110
                c = a++ / ++a * ++b + 10
                   = 100/102 * 11 + 10                                                  [Divide and multiply has the same
                   = 0 * 11 + 10                                                        precedence, so left associativity is
                   = 0 + 10                                                                                    applied]
                   = 10
              3.  If int a = 22, b = 20, c;
                c = (a * ++a % b + ++b) - a
                   = (22 * 23 % 20 + 21) - 23
                   = (506 % 20 + 21) - 23
                   = (6 + 21) - 23
                   = 27 - 23
                   = 4
              Based on the Arithmetic operators, two different concepts are rises. They are Counters and Accumulators.
              i.  Counters: Sometimes we need to know how many times a particular part of code has been executed. This is done
                 by the counter variable. Any variable which is initialized by 0 or 1 increases by 1 as the process starts repeating. It
                 should always be initialized to avoid a garbage value during execution. For example:
                          class use_of_counter
                            {
                               public static void main()
                               {
                                          int count=0,n=123;
                                          while(n>0)


                6262  Touchpad Computer Applications-X
   59   60   61   62   63   64   65   66   67   68   69