Вы находитесь на странице: 1из 11

OPERATORS

C++ supports a rich set of operators. We have used assignment


operator = so far. An operator is a symbol that tells the computer to
perform certain mathematical or logical manipulations. Operators are
used in programs to manipulate data and variables. They are usually
part of the mathematical or logical expressions. C++ operators
can be classified into following categories:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Arithmetic operators
The basic arithmetic operators are given below.

Operator Meaning

+ Addition or unary plus

- Subtraction of unary minus

∗ multiplication

/ Division

% Modulo division

Example 8
If a and b are two integers then:
a + b gives the sum of two integers a and b.
a * b gives their multiplication.
a / b gives the quotient when a is divided by b.
a % b gives the remainder when a is divided by b.

Here a and b are variables and are known as operands. The


modulo division operator cannot be used on floating point data.

Example 9
Expressions
Any arrangement of variables, constants and operators that specifies a
computation is called an expression. Thus
Sum + I j+6 total/6 I + k*p height%22
are all examples of expressions. When computations specified in the
expressions are performed, the result is usually a value. Thus if the
values of Sum and I are 45 and 10 respectively, the value of that
expression is 55.
Parts of expressions may also be expressions. Even single variables and
constants, like total and 6, are considered to be expressions.
Note that expressions are not the same as statements. Statements tell the
compiler to do something and terminate with a semicolon, while
expressions specify a computation. There can be several expressions in a
statement.
Distance = (x1*x1 – y1*y1) + (x1*x1 – y1*y1) + val*654;
Integer arithmetic
When both the operands in a single arithmetic expressions such as a + b
are integers, the expression is called an integer expression. And
the operation is called integer arithmetic. Integer arithmetic
always gives integer value.

Real arithmetic
An arithmetic operation involving only real operands is called real
arithmetic. Since floating point values are rounded to the number of
significant digits permissible, the final values is an approximation of the
correct result.

Mixed-mode arithmetic
When one of the operands is real and the other is integer, the expression
is called a mixed-mode expression. If one of the operand is of the
real type, then only real operation is performed and the result is always a
real number.
Thus,
15/10.0 = 1.5
But
15/10 = 1
Relational operators
We often compare two quantities and depending on their values, take
certain decisions. These comparisons can be done with the help of
relational operators.

Operator Meaning

< is less than

<= is less than or equal to

> is greater than

>= is greater than or equal to

== is equal to
!= is not equal to
Logical operators
C++ has following logical operators.

Operator Meaning

&& Means logical AND

|| Means logical OR

! Means logical NOT


Assignment operators
Assignment operators are used to assign the result of an expression to
a variable. We have seen the usual assignment operator, =. In
addition, C++ has a set of shorthand assignment operators of the
form
variable operator = expression;

Above assignment statement is equivalent to


variable = variable operator expression;

Example 10
Increment and decrement operators
C++ allows two very useful operators. These are the increment and
decrement operators:

++ and --
The operator ++ adds 1 to the operand, while -- subtracts 1. Both are
unary operators and takes the following form:

++m; or m++;
--m; or --m;
++m or m++ is equivalent to m = m + 1;
--m or m-- is equivalent to m = m - 1;
While ++m and m++ mean the same thing when they form
statements independently, they behave differently when they are
used in expressions on the right-hand side of an assignment
statement.
Example 11

Rules for ++ and – operators


Increment and decrement operators are unary operators and they
require variables as their operands.
When postfix ++ (or --) is used with a variable in an expression, the
expression is evaluated first using the original value of the variable
and the variable is incremented(or decremented) by 1.
When prefix ++ (or --) is used with a variable in an expression, first
the variable is incremented(or decremented) by 1 and then the
expression is evaluated using the new value of the variable.

Вам также может понравиться