Page 35 - Modular_V2.0_C++_Flikpbook
P. 35

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()
                 {                                                          DOSBox 0.74, Cpu speed: max 100% cycles,

                     clrscr();                                           Result of && operator is: 0
                     int p, q, r, s;                                     Result of ¦¦ operator is: 1
                                                                         Result of ! operator is: 1
                     p = 8;
                     q = 2;
                     r = 4;
                     s = 9;

                     cout<<"Result of && operator is: " << ((p>q) && (q>r))<<"\n";
                     cout<<"Result of || operator is: " << ((p>q) || (q>r))<<"\n";
                     cout<<"Result of ! operator is: " << !((p>q) && (r==s))<<"\n";
                     getch();

                 }
                 Assignment Operators

                 Assignment operators are used to assign the value of the right-hand operand to the left-hand
                 operand.  These operators are  also called  shorthand  assignment operators or  compound
                 assignment operators.





                                                                                                                  33
                                                                                               Operators in C++
   30   31   32   33   34   35   36   37   38   39   40