Page 266 - Computer_Science_F5
P. 266

(ii) Operator precedence in mathematical expressions
              Levels of precedence between operators, both in arithmetic expression and C++
              expressions,  remain  the  same.  That  level  starts  from  the  operation  enclosed  by
              brackets, followed by arithmetic operators out of the brackets. Priority is given to  Chapter Four: Object oriented programming with C++
              division and multiplication, followed by addition and subtraction. If the expression
          FOR ONLINE READING ONLY
              includes several arithmetic operators of the same type the operation will be conducted
              from left to right.


                   Activity 4.4:

                Writing mathematical expression using C++ programs

               Practice on how to write mathematical expressions in C++ programs by following
               the steps below:

               Step 1: Type the following expression in (a) word processor and (b) C++ text
                        editor. Compare the arithmetic operation typed in both (a) and (b).

                                  5 x  1  7                      4    5   x2  
                               z           a (   b2   )(c  y   )5  10     
                                     6     x                       x    y  1  

               Step 2:  If x=1, y= 2, a = 3, b = 5 and c=6, use a calculator to find the value of z.
               Step 3:  Write a C++ program to find the value of z using the values given in
                        step 2.

               Step 4:  Comment on the results obtained in step 2 and step 3.



              (b) Relational expressions
              These  compare two values and return  a boolean  result  (true  or false). Common
              relational operators include ==, !=, <, >, <=, and >=.
              For exampele, in program code:  bool isEqual = (a == b);


              (c) Logical expressions
              These combine multiple boolean expressions using logical operators such as && for
              (logical AND), || for (logical OR), and ! for (logical NOT).
              For example, in program code: bool isTrue = (a > b) && (c < d);

              (d) Bitwise expressions

              These operate on the binary representation of data using operators like & for (bitwise
              AND), | for (bitwise OR), ^ for (bitwise XOR), ~ for (bitwise NOT), << for (left
              shift), and >> for (right shift).


                                                    257
               Student’s Book  Form Five



     Computer Science Form 5.indd   257                                                     23/07/2024   12:33
   261   262   263   264   265   266   267   268   269   270   271