Page 260 - Computer_Science_F5
P. 260

Program Example 4.15:

                Assignment operations in C++

                                        #include<iostream>                                         Chapter Four: Object oriented programming with C++
                                        using namespace std;
          FOR ONLINE READING ONLY
                                        int main()
                                        {
                                                int a, b;
                                                a=34;
                                                b=22;
                                                a=b;
                                                b=7;
                                                cout<<”a = “<<a<<endl;
                                                cout<<”b = “<<b<<endl;
                                                return 0;
                                        }



                Output:






                                        Figure 4.31: Output of the program
               The output of the given program in Figure 4.34 shows that variable a assigned
               value of b, which is 22, and variable b replaced its value with 7.



              (c) Compound assignment operators
              If you want to change the value of the variable by conducting an operation on the
              value currently stored in the variable, you can use compound assignment operators.
              Table 4.8 shows compound assignment operators and their respective meanings.


              Table 4.8: Compound assignment operators
                       Operator                   Example                   Meaning
                          + =                      a + =b                    a = a +b

                           =                       a – =b                    a = a – b
                          * =                       a*=b                     a = a*b
                          / =                       a/=b                     a = a/b

                         % =                       a%=b                      a = a%b


                                                    251
               Student’s Book  Form Five



     Computer Science Form 5.indd   251                                                     23/07/2024   12:33
   255   256   257   258   259   260   261   262   263   264   265