Page 267 - Computer_Science_F5
P. 267

Computer Science  For example,  in program code:  int  Type conversion
           bitwiseResult = b & c;
                                                     During assignment,  if the  types of the
                                                     variable and the expression do not match,
           Function call expressions
                                                     implicit or explicit type conversion may
           These involve  calling  a function  and
          FOR ONLINE READING ONLY
           using its return value in an expression.
           An example of function call expression    occur. For example, in program code:
                                                        double x = 5; // Implicit conversion
           is like program code: int result = sqrt(a)   from int to double
           + pow(c, d);
                                                        int y = static_cast<int>(x); //

           Assignments                                  Explicit  conversion from double to
           Assignments in C++ are used to store the     int
           result of an expression into a variable.
           The assignment operator (=) is the most   Lvalue and Rvalue
           common operator used for this purpose.  The variable on the left side of the
           Here are some important aspects of        assignment operator is called an lvalue
           assignments:                              (left value), this refers to a location in

                                                     memory.  The expression on the right
           Basic assignment                          side is an rvalue  (right  value),  this is
           The value on the right-hand side of the
           = operator  is evaluated  and  stored  in   a  value  that  have  no  specific  memory
           the variable on the left-hand side. For   location. For example, in program code:
           example, in program code: int a = 12;     int a = 10; // ‘a’ is an lvalue, ‘10’ is an
                                                     rvalue
           Compound assignment operators
           These operators combine an arithmetic         Activity 4.5:
           operation with an assignment. Examples
           include  +=, -=, *=, /=, and %=. For       Creating a program that combines
           example, in program code:                  expressions and assignments
           int a = 14;
           a += 7; // Equivalent to a = a + 7;        Use the knowledge you have learnt
                                                      in previous sections, create a simple
           Chained assignment                         program  that  combines  expressions

           Multiple assignments can be chained        and assignments in a C++ program.
           together,  allowing  the  same  value  to   Once you have created, highlighting
           be assigned to multiple  variables.  For   how they are used to manipulate and
           example, in program code:
           int a, b, c;                               store data in a C++ program
           a = b = c = 20;

                                                 258
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   258                                                     23/07/2024   12:34
   262   263   264   265   266   267   268   269   270   271   272