L10-Operators and Expressions in C++
L10-Operators and Expressions in C++
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.
EXPRESSION
EVALUATION
In C++, mixed expression is evaluated as
follows:-
1. It is first divided into component sub-expressions
up to the level of two operand s and an operator.
2. Then the type of the sub-expression is decided
using earlier stated general conversion rules.
3. Using the results of sub-expressions, the next
higher level of expression is evaluated and its type
is determined.
4. This process goes on until you have the final
result of the expression.
LOGICAL EXPRESSION
DEF…
The expressions that results into
true(1) or false(0) are called logical
expressions.
ASSIGNMENT STATEMENT
Assignment statement assigns a value to a
variable. In C++,
Equal to (=) symbol is an assignment
statement.
For example:
A=8495;
B=564.44;
C++ SHORT HANDS
Different Types of Assignment statements
are used while programming and they are,