C++ Operators - IV


The following topics are covered in this section:


5. Logical Operators - AND ( && ) OR ( || ) NOT (!)

These operators are used to combine two or more expressions. The way in which they combine the expression differs depending on the operation used. They are used when we need to test multiple conditions. For example you may write a program that has to check whether the marks scored by a student is greater than 70 and less than 80. If it is so then you will want the program to display a ‘B’ grade. To check whether the average mark is greater than 70 you have to use one expression and to check whether the average is less than 80 you should use another expression. Thus in simple English your statement will be:

If (average mark is greater than 70 AND average mark is less than 80)

Print "B grade"

AND: it combines two conditional expressions and evaluates to true only if both the conditions are true.

First Condition

Second condition

Result of AND operation

False

False

False

False

True

False

True

False

False

True

True

True

If the first condition and the second condition are both true then the result of the AND operation will also be true.

Example:

 

      // To check whether the given number is even
      # include <iostream.h>
      int main ( )
      {
          int num;
          cout<< "Enter the number";
          cin>>num;
          if ( (num!=0) && ((num%2)= =0) )                 // Two conditions have to be true
          {
              cout<<"\n Even Number";
          }
          return 0;
      }

In this program we need to check for two conditions (the number entered should not be zero and the number when divided by 2 should not produce a remainder). Only if both these conditions are satisfied should the program display that the number is even. The AND operator is used to combine the two conditions that are to be tested and if both are true then the message is displayed.

OR: operator combines two conditions and evaluates to true if any one of the conditions is fulfilled (i.e. only one of the conditions need to be true). It is designated by using two parallel bars/pipes ( | | ).

First Condition

Second condition

Result of OR operation

False

False

False

False

True

True

True

False

True

True

True

True

 

The ‘AND’ and ‘OR’ operators can be used on a sequence of conditions (i.e. at a time you can check for multiple conditions). For example the following code is legal:

In this case only if all the four conditions are true will the body of the ‘if condition’ be executed.

NOT: NOT is a unary operator. Unlike ‘AND’ and ‘OR’, NOT operates only on one operand. The logical value of the operand is reversed. If the operand is true, then after the NOT operation it will be become false. You might be thinking that the NOT operator can operate only on 1 and 0. Actually, any number greater than 0 will be considered as a true value. Hence the following would give:

Condition

Result of NOT operation

False

True

True

False

Basically, any number other than zero is considered as true. ‘Not’ of any number (other than 0) will give you FALSE (or zero). Check out the following program that illustrates the NOT operator.

      #include<iostream.h>
      int main ( )
      {
          int num, result;
          cout<< "Enter the number";
          cin>>num;
          result = (!num);
          cout<<result;
          return 0;
      }

 


6. Unary Operators (NOT operator !, increment operator ++, decrement operator --)

Unary operators operate on only one operand, which could be a constant or a variable. We’ve already discussed about the NOT operator. Another simple unary operator is the unary minus operator. This will act on only one operand and will change the sign of the number it operates on. For example:

The ++ and – operator are very important if you are taking a course in C++ (teachers usually love this topic and they are bound to ask some questions on unary operators).

++ is known as the increment operator and it can be used in two ways.

If we write:

This is the same as:

At the end of these steps the value of both i1 and i2 will be 11. When used as a prefix, the variable is first incremented and later assigned.

If we write:

This is the same as writing:

At the end of these steps the value of i2 will be 10 and that of i1 will be 11. When used as a postfix, the variable value is first assigned and later incremented.

The decrement operator works in the same way. The only difference is that it decreases the value of the operand by one. Again the decrement operator also can be used in two ways: as a prefix or as a suffix (or postfix).

Suppose we have the following coding in a program, what will be the value of ‘sum’ at the end of each statement?

Thus in the case of the postfix operator the value is assigned first and increment is carried out in the next step. If you make use of the prefix operator then:

Remember: When used as a prefix ( ++i ) compiler will first increment and then assign. When used as a suffix, assignment is done first and then incrementing is performed.

Beware: The ++ and – operators cannot be used on expressions (i.e. ++ (y + 1); is an incorrect statement and will lead to a compiler error).


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.