Page 262 - Computer_Science_F5
P. 262

The    following    Program     Example    4.17    (e) Relational operators
              demonstrates the use of increment and decrement    Sometimes, in programming,
              operators. The output is shown in Figure 4.33.     we need to compare two values

                                                                 to know if they are equal or not,  Chapter Four: Object oriented programming with C++
                      Program Example 4.17:
          FOR ONLINE READING ONLY
                                                                 greater than or less than. The
                Increment and decrement operators                values can either be variables,

                                                                 constants or expressions.
                     #include<iostream>
                     using namespace std;
                     int main()                                  C++ programming language
                     {                                           evaluates the comparison
                            int x = 25;                          between two values using
                            cout<<”x++ = “<<x++<<endl;           relational operators. Relational
                            cout<<”x-- = “<<x--<<endl;           operators can help you in
                            cout<<”++x = “<<++x<<endl;
                            cout<<”--x = “<<--x<<endl;           making the decision.  You
                            return 0;                            can use relational operators
                     }                                           to compare the height of two

                                                                 trees, the ages of two students
                Output:
                                                                 or the prices of two items.
                                                                 Usually, the result obtained
                                                                 after evaluating relational
                                                                 operators is either true or false.
                                                                 Table 4.10 shows relational

                                                                 operators,  their  meaning,
                       Figure 4.33: Output of the program
                                                                 examples and return values.

              Table 4.10: Relational operators

               Operator Meaning                     Example      Evaluation    Return value
               <           less than                16 < 20      True          1

               < =         less than or equal to    10 < = 10    True          1
               >           greater than             5 > 11       False         0

               > =         greater than or equal to  45 > = 28   True          1
               = =         equal to                 6 = = 7      False         0

               ! =         is not equal to          8! = 8       False         0




                                                    253
               Student’s Book  Form Five



     Computer Science Form 5.indd   253                                                     23/07/2024   12:33
   257   258   259   260   261   262   263   264   265   266   267