|
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 |
|