C++ Learning Resource Network!

 
 
 C++ Tutorials  >  Introduction to C++  >  Operators
 

All computer languages provide tools for some predefined operators. These tools are known as operators. The various categories of operators are:

> Arithmetic operators

> Relational operators

> Logical operators

Arithmetic Operators

In addition to the four arithmetic operators +, -, *, /, C++ also provides the remainder or the modulo operator represented by the % symbol. The modulo operator returns the remainder when an integer is divided by another. The following program illustrates the usage of the modulo operator.

//Program 1.9
//This program demonstrate the use of modulo operator
#include<iostream.h>
void main()
{
int Var1 = 11, iVar2 = 5, iVar3;
iVar3 = iVar1 % iVar2;
cout<<"The remainder is:"<<iVar3<<endl;
}


The output of Program 1.9 is:

The remainder is: 1

The modulo operator works only with integers.

C++ offers a condensed approach to perform calculations using arithmetic assignment operators which combine an arithmetic operator (+, -, /, %) and the assignment operator(=). The statement,

iCount = iCount + 2;

increments the value of the variable iCount by 2. The above statement can be modified as,

iCount += 2;

using the arithmetic assignment operator (+=).

Other arithmetic assignment operators are: -=, *=, /= and %=.

Relational Operators

Relational operators are used to compare tow values. The result of the comparison is either true(1) or false(0).

Table 1.2 lists the various relational operators.

 

Operator Meaning
>  greater than
<  less than
= =

 equal to

!=  not equal to
>=  greater than or equal to
<=  less than or equal to

Table 1.2 Relational Operators

 
Logical Operators

Logical operators are used to combine two or more test expressions. C++ provides the AND(&&), the OR(||) and the NOT(!) logical operators. The && logical operator combines two conditional expressions and evaluates to true only if both conditions are fulfilled. The following program illustrate the usage of the && logical operator.

//Program 1.10
//This program demonstrate the usage of the && operator
#include<iostream.h>
void main()
{
int iTemp1;
cout<<"Enter a Number:"<<endl;
cin>>iTemp1;
if((iTemp1 != 0) && ((iTemp1 % 2) == 0))
{
cout<<"Even number";
}
else
{
cout<<"Odd number or number is zero";
}
}


The if statement in Program 1.10 displays:

Even number

only if both the condition are true. Otherwise, the statement following else is exected.

The || logical operator combines two conditional expressions, and evaluates to true if any one condition is fulfilled.

The following program demonstrates the usage of the || logical operator.

//Program 1.11
//This program demonstrates the usage of the || logical operator.
#include<iostream.h>
void main()
{
char cVar;
cout<<"Enter A or a:";
cin>>cVar;
if(cVar == 'a' || cVar == 'A')
{
cout<<"You have entered the right character";
}
else
{
cout<<"The character is not A or a";
}
}
 

| Bookmark This Site |   | Links |   | Search |   | Submit |   | Contact |

  Copyright 2008 - 3008  CppTutor.com All rights reserved.