Page 63 - iPlus_Ver_2.0_class_8
P. 63

Assignment Operators

                 Assignment operators are used to assign values to operands. One of the most commonly used
                 assignment operators is equal (=). This operator can also be used with arithmetic operators
                 to  make the  shorthand  assignment operators.  The  shorthand  assignment operators  perform
                 calculations and assign the result to the variable present on the left hand side of the operator.
                                                                                           Example
                  Operator                   Description                   Syntax                          Output
                                                                                      (int a = 10, b = 2)

                              Returns the result to the operand present
                                                                                            a += b          a = 12
                     +=       on  the  left  hand  side after  performing   a += b
                                                                                            a += 5          a = 15
                              the addition operation.
                              Returns the result to the operand present
                                                                                            a -= b           a = 8
                     -=       on  the  left  hand  side after  performing    a -= b
                                                                                            a -= 5           a = 5
                              the subtraction operation.
                              Returns the result to the operand present
                                                                                            a *= b          a = 20
                     *=       on  the  left  hand  side after  performing   a *= b
                                                                                            a *= 5          a = 50
                              the multiplication operation.
                              Returns the result to the operand present
                                                                                            a /= b           a = 5
                     /=       on  the  left  hand  side after  performing   a /= b
                                                                                            a /= 5           a = 2
                              the division operation.
                              Returns  the  remainder  to  the  operator
                                                                                            a %= b           a = 0
                     %=       present  on the left hand  side after        a %= b
                                                                                            a %= 6           a = 4
                              performing the division operation.


                                                                                            #Digital Literacy
                      Let’s CatCh Up


                   Name any three types of literals








                  i +  WRITING SOME MORE PROGRAMS

                 Let us use the various concepts of Java to write programs.

                 Program 1: Write a program to calculate the area and perimeter of a rectangle.
                 public class Calculate {
                        public static void main(String args[]) {

                               int length, width, area, perimeter;
                               length = 5;
                               width = 10;
                               // Area of rectangle = length * width
                               area = length * width;                                            Output


                                                                                                                  61
                                                                                               Program Coding
   58   59   60   61   62   63   64   65   66   67   68