Page 101 - CA_Blue( J )_Class9
P. 101

In the above program, the variable “s” is an accumulator. As the value of i increases by 1, the variable “s” adds the
                 value of i.
                 So, the output is Sum: 165
                 Let us take a look at the difference between a counter and an accumulator.

                                      Counter                                         Accumulator

                 A  counter  is a numeric variable used to count  An accumulator is  a numeric variable  used  to
                 occurrences or iterations.                         accumulate (sum up) values.

                 Typically increases  by  a fixed amount,  such as 1,  on  Adds varying values together as specified in the loop.
                 each iteration.

                 int counter = 0;                                   int sum = 0;
                                                                    for (int i = 10; i <= 20; i++)
                 for (int i = 1; i <= 10; i++) {
                                                                    {
                        counter++;  }
                                                                        sum += i;
                 System.out.println("Loop executed " + }
                 counter + " times.");                              System.out.println("The sum is: " + sum);

                 5.3.4 Relational Operators
                 Relational operators compare two variables or expressions and return a boolean value (true or false) based on
                 the relationship between them. These operators work with numeric data types (e.g., int, float, double) and char.
                 However, they cannot be directly used on String types for comparison to compare strings.

                                                                                  Example
                           Sl. No.      Operator Name       Operator Symbol                        Output
                                                                               int m=50, n=40.
                             1.    Greater than                    >               m>n              true
                             2.    Greater than and Equal to       >=              m>=n             true
                             3.    Less than                       <               m<n              false
                             4.    Less than and Equal to          <=              m<=n             false
                             5.    Equal to                        ==              m==n             false
                             6.    Not Equal to                    !=              m!=n             true


                  Program 17     Write a program to show the use of all the relational operators.

                   1  class relational_operator

                   2  {
                   3      public static void main()

                   4      {
                   5          int a =7,b=11;
                   6          System.out.println("a is "+a+" b is "+b);

                   7          System.out.println("Answer of == operator : "+(a==b));

                   8          System.out.println("Answer of != operator : "+(a!=b));
                   9          System.out.println("Answer of > operator : "+(a>b));
                  10          System.out.println("Answer of < operator : "+(a<b));



                                                                                               Operators in Java   99
   96   97   98   99   100   101   102   103   104   105   106