Professional Documents
Culture Documents
Structured Programming Slide Week3
Structured Programming Slide Week3
2/26/2013
They include:
a) Mathematics operator
b) Assignment operator
c) Relational operator
d) Logical operator
e) Additional operators
Example
Binary Operations Results Remarks
2*3
Do not use x
15 / 5
10 % 4
4 + 10
14
3-8
-5
2/26/2013
What is an assignment
operator?
Assigns a value (rvalue) to a variable (lvalue)
using equal (=)
i) x=y=z=100;
ii) sum=2+(value=7-x);
b) compound assignment
Example
Expressions
4 == 0
1 != 9
10 < 10
8 >= 2
(a=2) <= 5-4
2*5 > 9/3
Decision
false
true
false
true
false
true
2/26/2013
Example
Expression
( (2==2) && (5<10) )
( ( (a=2)>5 ) && (6<=6) )
( (5!=3) || (1>=2) )
( (4>5*3) || ((b=5/2)==2) )
! (2>=7)
! ( ( (4>9) && (a==0) ) || (6<=13) )
Truth
Tables
2/26/2013
Conditional Operator
Used to replace if-else logic
if-else
if (x > z)
{ sum=5;}
else
{ sum=1;}
?:
(x > z) ? (sum=5) : (sum=1)
OR
sum=(x > z) ? (5) : (1)
Increment/Decrement
Operators
Increment/decrement by 1 value stored in a
variable can be done using (++) or (--)
Increment
X=X+1;
X+=1;
X++;
++X;
When an increment/decrement
occurs?
Example
2 rules:
a) If a variable is assigned with a prefix
x= 9;
y= ++x;
cout<<y=<< x<<endl;
cout<<x=<<y<<endl;
Expression
x=9;
y=x++;
cout<<y=<<y<<endl;
cout<<x=<<x<<endl;
Decrement
X=X-1;
X-=1;
X--;
--X;
Output
y=10
x=10
y=9
x=10
2/26/2013
Example
Comma Operator
Expression
x= 9;
y= ++x -2;
cout<<y=<< y<<endl;
cout<<x=<<x<<endl;
Output
y=8
x=10
x=9;
y=x++ -2;
cout<<y=<<y<<endl;
cout<<x=<<x<<endl;
y=7
x=10
Unary Operator
An operator that affects a single value
Denotes signed of numbers:
a) positive (+)
b) negative (-)
Example:
a) x= +5
b) y= -x
Using (,)
x= (y=10, y-3);
b=2;
a=b*3 +1;
Sizeof Operator
A compile-time operator that operates on a
single value.
Produces a result that represents size in byte
of data or data type specified.
Enables program to maintain consistency on
different types of computers.
Example:
a) sizeof (A);
b) sizeof (string);
2/26/2013
Bitwise Operators
Bitwise Operators
Perform a bit-by-bit comparison with internal
data to produce desired results.
Applied on character and integer variable/literals
but NOT floating point data.
Used in binary representation of integer data.
Used in advanced programming techniques.
Enable programmers to manipulate internal bits
in memory/variables.
Enable programmers to improve efficiencies of
their programs in several ways.
2/26/2013
Order of Precedence
Rank Operator
Details
Grouping
+-
additive
Left-to-right
<< >>
shift
Left-to-right
relational
Left-to-right
== !=
equality
Left-to-right
11
&
bitwise AND
Left-to-right
12
bitwise XOR
Left-to-right
13
bitwise OR
Left-to-right
14
&&
logical AND
Left-to-right
15
||
logical OR
Left-to-right
16
?:
conditional
Right-to-left
17
assignment
Right-to-left
18
comma
Left-to-right
scope
Left-to-right
() [] . -> ++ -- dynamic_cast
static_cast reinterpret_cast
const_cast typeid
postfix
Left-to-right
unary (prefix)
*&
indirection and
reference
(pointers)
+-
unary sign
operator
Right-to-left
(type)
type casting
Right-to-left
.* ->*
pointer-tomember
Left-to-right
*/%
multiplicative
Left-to-right
<cmath> Operations
10
Grouping
::
Order of Precedence
Rank Operator
Details
2/26/2013
Power Functions
Functions
Power
Examples
210
533
54
Square root
C++ Syntax
pow (2, 10)
pow (5, pow (3,2))
pow (5,(3/4))
sqrt (5)
Hyperbolic Functions
Functions
Sinh
Cosh
Tanh
Examples
sinh (30.0)
cosh (30.0)
tanh (30.0)
C++ Syntax
sinh (30.0)
cosh (30.0)
tanh (30.0)
Trigonometric Functions
Functions
Sine
Cosine
Tangent
Arc Sine
Arc Cosine
Arc Tangent
Examples
sin (30.0)
cos (30.0)
tan (30.0)
sin-1 (30.0)
cos-1 (30.0)
tan-1 (30.0)
C++ Syntax
sin (30.0)
cos (30.0)
tan (30.0)
asin (30.0)
acos (30.0)
atan (30.0)
Examples
e 2.45
log10 (2.45)
ln (2.45)
2.45=2.00+0.45
C++ Syntax
exp (2.45)
log10 (2.45)
log (2.45)
modf (2.45)
2/26/2013
Other Functions
Functions
Absolute value
Absolute value
for floating no.
Round down
Round up
Remainder
Examples
|-3|
|5.456671100|
C++ Syntax
abs (3)
fabs (5.456671100)
2.3 = 2.0
-2.3 = -3.0
3.8 = 4.0
-3.8 = -3.0
5.3/2 = 1.3
floor (2.3)
floor (-2.3)
ceil (3.8)
ceil (-3.8)
fmod (5.3, 2)