Page 263 - Computer_Science_F5
P. 263

Computer Science  The following Program Example 4.18 demonstrates the use of relational operators.
           The output of the program is shown in Figure 4.34.

                   Program Example 4.18:

             Use of relational operators
          FOR ONLINE READING ONLY
                     #include <iostream>
                     using namespace std;
                     int main()
                     {
                            bool result;
                            cout<<”Condition : Return value”<<endl;
                            result = (5 == 5);
                            cout<<”5==5”<<”   :      “<< result<<endl;
                            result = (8>13);
                            cout<<”8>13”<<”   :      “<< result<<endl;
                            result = (22<=12);
                            cout<<”22<=12”<<” :      “<< result<<endl;
                            result = (15!=17);
                            cout<<”15!=17”<<” :      “<< result<<endl;
                            return 0;
                     }
             Output:











                                     Figure 4.34: Output of the program

           (f) Logical operators

           A relational operator involves only one condition at a time. To combine two or more
           conditions, you can use logical operators. Logical operators include, !, | | and &&
           operator.  Table 4.11 shows logical operators, their meaning, examples and return
           values.


           Table 4.11: Logical operators
            Operator       Meaning             Example                    Return value
            !              Logical NOT         45! = 67                   1
            |  |           Logical OR          (6 > 3) | | (8! = 8)       1
            &&             Logical AND         (5 < = 3) && (9 > = 5)     0


                                                 254
                                                                for Advanced Secondary Schools



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