Page 27 - Modular_V1.1_Flipbook
P. 27

Logical Operators

                 Logical operators are used to combine more than one relational expression. 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(){
                 int p, q, r, s;
                 p = 8;
                 q = 2;

                 r = 4;
                 s = 9;
                 clrscr();
                 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. C++ provides the following assignment operators:

                  Operator              Name                             Description                     Example
                      =       Assignment                  It assigns the value of the right operand        x = 5
                                                          to the left operand.
                      +=      Addition assignment         It adds right operand to the left operand       x += 3
                                                          and  assigns the  result  to  left  operand.

                                                          x+=3 is equivalent to x=x+3.





                                                                                               Operators in C++   25
   22   23   24   25   26   27   28   29   30   31   32