Page 99 - CA_Blue( J )_Class9
P. 99

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

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

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

                 Here, the value of b=5 is kept in working memory, and then it is decreased by 1. So, it becomes 4. After that it is
                 multiplied with the value in the working memory. The result becomes 20.


                 5.3.3 Counters and Accumulators
                 Based on the Arithmetic operators, two different concepts rise. They are Counters and Accumulators.

                 Counters
                 A counter is a variable used to track how many times a particular part of code is executed. Counters are especially
                 useful in loops when counting iterations or specific occurrences. Generally, a counter variable is initialized to
                 0 (or 1 depending on the context) and is incremented (usually by 1) each time the associated code block runs.
                 It is crucial to initialize  the counter  to avoid  any  unexpected or "garbage"  values that could  affect  accuracy.
                 For example, we want to check whether a given number is a prime number or not.


                  Program 15     Write a program to determine if a number is prime using counters.


                   1  class check_prime
                   2  {
                   3  public static void main()

                   4      {
                   5           int count=0,n=47,i;

                   6           for(i=1;i<=n;i++)
                   7           {

                   8                if(n%i==0)
                   9                {

                  10                    count=count+1;
                  11                }

                  12           }
                  13                if(count==2)

                  14                    System.out.println(n+ " is a prime digit");
                  15                else

                  16                    System.out.println(n+ " is not a prime digit");
                  17     }}








                                                                                               Operators in Java   97
   94   95   96   97   98   99   100   101   102   103   104