Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 144 - CoreGPT(Podar)_Flipbook_C8
P. 144

    Program 3: To use all the relational operators.

                      #include<iostream.h>
                      #include<conio.h>
                      void main()
                      {
                                                            Result of equal to operator is: 0
                          clrscr();
                                                            Result of greater than operator is: 1
                          int a, b;                         Result of less than operator is: 0
                          a = 75;                           Result of greater than equal to operator is: 1
                                                            Result of less than equal to operator is: 0
                          b = 65;
                                                            Result of not equal to operator is: 1_

                          cout<<"Result of equal to operator is: " << (a == b) << '\n';
                           cout<<"Result of greater than operator is: " << (a > b) << '\n';
                          cout<<"Result of less than operator is: " << (a < b) << '\n';
                           cout<<"Result of greater than equal to operator is: " << (a >= b)
                            << '\n';
                           cout<<"Result of less than equal to operator is: " << (a <= b) <<
                            '\n';
                          cout<<"Result of not equal to operator is: " << (a != b);
                          getch();

                      }
                      Logical Operators

                      Logical operators are used to combine more than one relational expression and return a boolean
                      result (true or false). C++ provides the following logical operators:

                        Operator      Name                 Description                  Example (x=2)           Output
                           &&          AND      It  returns  true,  if  both  operands   (x < 5) && (x < 10)     TRUE

                                                are true.
                            ||          OR      It  returns  true,  if  one  of  the     (x < 5) || (x < 2)      TRUE
                                                operands is true.
                             !         NOT      It reverses the result, returns false,  ! [(x < 5) && (x < 10)]  FALSE
                                                if the result is true or vice versa.

                      The NOT operator is unary operator as it requires only one operand to work.
                      Program 4: To use logical operators.

                      #include<iostream.h>
                      #include<conio.h>

                      void main()
                      {
                                                                              Result of && operator is: 0
                          clrscr();                                           Result of ¦¦ operator is: 1
                                                                              Result of ! operator is: 1
                          int p, q, r, s;
                          p = 8;

                    142     Premium Edition-VIII

| 139 140 141 142 143 144 145 146 147 148 149 |