8 Operators and Expressions in C++
8 Operators and Expressions in C++
PRAVEEN M JIGAJINNI
PGT (Computer Science)
MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc.
& Engg.
Reference Book
CLASS XI
By
Sumita Arora
CHAPTER 8
OPERATORS
AND
EXPRESSIONS
IN C++
OPERATORS
The operations (specific task) are
represented by operators and the
objects of the operation(s) are
referred to as operands.
OPERATORS
• Of the rich set of operators c++
provides here we shall learn some.
They are:--
1. Arithmetic operators
2. Increment/decrement operators
3. Relational operators
4. Logical operators
5. Conditional operators
6. Some other operators
1) ARITHMETIC OPERATORS
C++ provides operators for five (5)
basic arithmetic operations :-
1.Addition (+)
2.Subtraction (-)
3.Multiplication (*)
4.Division (/)
5.Remainder (%)
ARITHMETIC OPERATORS
NOTE: Each of these is a binary
operator i.e., it requires two values to
(operands) to calculate a final answer.
But there are two unary
arithmetic operators (that requires
one operand or value) viz.
Unary +
Unary -
UNARY OPERATORS
Operators that act on one
operand are referred to as unary
operators.
UNARY +
The operator unary ‘+’
precedes an operand. The operand (the
value on which the operator operates) of
the unary + operator must have
arithmetic or pointer type and the result
is the value of the argument.
For ex:-
If a=5 then +a means 5.
If a=0then +a means 0.
If a=-4 then +a means -4.
UNARY -
The operator unary ‘–’ precedes an
operand. The operand of an unary –
operator must have arithmetic type and
the result is the negation of its
operand’s value.
For ex:-If a=5 then -a means -5.
If a=0then -a means 0 (there is no
quantity mnown as –0).
If a=-4 then a means 4.
This operator reverses the sign of the
operand’s value.
BINARY OPERATORS
Operators that act upon two
operands are referred to as binary
operators.
Precedence of operators
++(post increment),--(post decrement)
++(pre decrement), --(pre increment)
*(multiply),/(divide),%(modulus)
+(add),-(subtract)
<(less than),<=(less than or equal ),>(greater than),>=(greater than
or equal )
==(equal ), !=(not equal)
&&(logical AND)
||(logical OR)
?:(conditional expression)
+(simple assignment) and other assignment operators (arithmetic
assignment operator)
Comma operator
EXPRESSIONS
An expression in C++ is any valid
combination of operators, constants and
variables.
EXPRESSION
The expression in C++ can be of any
type i.e., relational, logical etc.
Type of operators used in an
expression determines the type of
expression.
ARITHMETIC
EXPRESSIONS
Arithmetic expression can either be
integer or real expressions.
Sometimes a mixed expression can
also be formed which is a mixture of real
and integer expressions.
INTEGER EXPRESSIONS
Integer expression is formed by connecting integer
constants and/or integer variables using integer arithmetic
operators.
Following expressions are valid integer expressions:--
const count =30;
int i, j, k, x, y, z;
a) i
b) -i
c) k-x
d) k+x-y+count
e) -j+k*y
f) j/z
g) z%x
REAL EXPRESSIONS
Real expressions are formed by connecting real
constants and/or real variables using real arithmetic
operators (e.g., % is not a real arithmetic operator ).
The following are valid real expressions :--
const bal=250.53;
float qty,amount,value;
double fin,inter;
i. qty/amount
ii. qty*value
iii. (amount +qty*value)-bal
iv.fin+qty*inter
v. inter-(qty*value)+fin
RULE
An arithmetic expression may
contain just one signed or unsigned
variable or a constant, or it may have two
or more variables and/or constants, or two
or more valid arithmetic expressions joined
by a valid arithmetic operators. Two or
more variables or operators should not
occur in continuation.
REAL EXPRESSIONS
CONTD..
Apart from variables and
constants and arithmetic operators, an
arithmetic expression may consist of C+
+’s mathematical functions that are part of
c++ standard library and are contained in
header files math.h . To use the
mathematical functions, you must include
math.h.
MATH.H FUNCTIONS
The general form:--
Functiontype functionname (argument list);
Functiontype-specifies type of value
returned by the functionname
argument list-specifies
arguments and their data type (separated
by commas as in int a, real b, float d,) as
required by the functionname.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype Description example
no. ctio
n
1 aco double acos (double the acos () function Diuble val=-
s arg) returns the arc cosine of 0.5;
arg. The argument to cout<<acos
acos () must be in the (val);
range of -1 t o +1;
otherwise a domain error
occurs.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
2 asin double asin(double The asin() function double val=-
arg) returns the arc sine of 10;cout<<asi
arg. The argument to asin n (val);
must be in the range -1 to
1.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
3 atan double asin(double The atan() function atan (val);
arg) returns the arc tangent of (val is a
arg. double type
identifier)
MATHEMATICAL FUNCTIONS
IN math.h
s. Fun prototype description example
no. ctio
n
4 atan double atan2(double The atan2() function couble val=-
2 b, double a) returns the arc tangent of 1.0;cout<<at
b/a. an2(val,1.0);
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
5 ceil double ceil(double The ceil() function ceil(1.03)
num) returns the smallest gives 2.0
integer represented as a ceil(-1.03)
double not less than num. gives -1.0 .
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description Example
no. ctio
n
6 cos double cos (double The cos() function cos(val) (val
arg) returns the cosine of arg. is a double
The value of arg must be type
in radians. identifier)
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
7 cos double cosh(double The cosh() function Cosh(val)
h arg) returns the hyperbolic (val is a
cosine of arg. The value double type
of arg must be in radians. identifier.)
MATHEMATICAL FUNCTIONS
IN math.h
s. Fun prototype Description example
no. ctio
n
8 exp double exp(double The exp() function Exp(2.0)
arg) returns the natural gives the
logarithm e raised to value of e2.
power arg.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description Example.
no. ctio
n
9 fabs double fabs(double The fabs() function fabs(1.0)
num) returns the absolute value gives 1.0
of num. fabs(-1.0)
gives 1.0.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
10 floor Double floor(double The floor() functions Floor(1.03)giv
num) returns the largest integer es 1.0 floor (-
(represented by double) 1.03) gives -
not greater than num. 2.0.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
11 fmo double fmod(double The fmod() function fmod(10.0,
d x, double y) returns the remainder of 4.0) gives
the division x/y. 2.0.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
12 log double log(double The log() function returns log(1.0) gives
num) natural logarithm for the natural
num. a domain error logarithm for
occur if num is negative 1.0.
and a range error occur if
the arg num is 0.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
13 log1 double log10(double The log10() function log 10(1.0)
0 num) returns the base10 gives base 10
logarithm for num. A logarithm for
domain error occurs if 1.0.
num is negative and
range error occurs if the
argument is 0.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
14 pow double pow(double The pow() function pow(3.0, 1)
base, double exp) returns the base raised to gives 3.0.
the power exp. A domain
error occurs if the base is
0 and exp<=0. also if
base<0 and exp is not an
integer.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
15 sin double sin(double The sin() function returns Sin(val) ( val
arg) the sin of arg. The value is a double
of arg must be in radians. type
identifier).
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
16 sinh double sin (double The sinh() function Sinh(val)
arg) returns the hyperbolic (val is a
sine of arg. The value of double type
arg must be in radians. identifier).
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
17 sqrt double sqrt(double The sqrt() function Sqrt(4.0)
num) returns the square root of gives 2.0.
the num. A domain error
occur if num<0.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
18 tan double tan(double The tan () function tan(val)
arg) returns the tangent of
arg. The value of arg
must be in radians.
MATHEMATICAL FUNCTIONS
IN math.h
s. fun prototype description example
no. ctio
n
19 tan double tanh(double The tanh() function tanh(val)
h arg) retuns the hyperbolic
tangent of arg . The arg
must be in radians.
TYPE CONVERSION
Def…