Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

SJES 2271/ SJEM 2231

2/26/2013

What are C++ operators?


 Signs/symbols referred to a specific task or
operation to be performed in C++

SJES 2271 : Scientific Computing 1


SJEM 2231 : Structured Programming
LECTURE NOTES (WEEK 3)
BY
DR NOOR FADIYA MOHD NOOR

What are primary mathematics


operators?

DR NOOR FADIYA MOHD NOOR, UM

 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

For integer values,


remainder is
discarded
For integer values
only

4 + 10

14

3-8

-5

SJES 2271/ SJEM 2231

2/26/2013

What is an assignment
operator?
 Assigns a value (rvalue) to a variable (lvalue)
using equal (=)

What are compound assignment


operators?
 Operators to perform modification on current
stored values

 Can be done in 2 ways:


a) multiple assignment i.e.

i) x=y=z=100;
ii) sum=2+(value=7-x);
b) compound assignment

What are relational


operators?
 Operators used for data comparison that
return true or false.

DR NOOR FADIYA MOHD NOOR, UM

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

SJES 2271/ SJEM 2231

2/26/2013

What are logical operators?


 Compound relational operators for advance
data comparison.

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

DR NOOR FADIYA MOHD NOOR, UM

Truth
Tables

What are additional C++


operators?
Decision
true
false
true
false
true
false

 Other operators that enable a C++ program to


be run efficiently are
a) conditional operator (?:)
b) increment/decrement operator
c) comma operator (,)
d) type casting operator
e) unary operator
f) sizeof operator
g) bitwise operator

SJES 2271/ SJEM 2231

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;

operator (++a,--a) the change occurs before


the next operation.
b) If a variable is assigned with a postfix
operator (a++,a--) the change occurs after
the next operation.

DR NOOR FADIYA MOHD NOOR, UM

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

SJES 2271/ SJEM 2231

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

DR NOOR FADIYA MOHD NOOR, UM

 Used to separate 2 or more expressions


expected to return 1 value
Normal
y=10;
x=y-3;

Using (,)
x= (y=10, y-3);

b=2;
a=b*3 +1;

a = (b=2, 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);

SJES 2271/ SJEM 2231

2/26/2013

Type Casting Operator


 Used to temporarily change a variables
declared data type to a new one.
 2 formats:
a) (data type) expression

b) data type (expression)


 Example:
a) age= (double) age*factor;
b) age= double (age)* factor

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.

Truth Tables: Bitwise Operators

# bits binary digits (0 and 1)

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

2/26/2013

What is the order of


precedence?

Order of Precedence
Rank Operator

 Math hierarchy or order of operators that decide


which operand is evaluated first in C++.
 Example of order of precedence of primary
maths operators:

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

DR NOOR FADIYA MOHD NOOR, UM

scope

Left-to-right

() [] . -> ++ -- dynamic_cast
static_cast reinterpret_cast
const_cast typeid

postfix

Left-to-right

++ -- ~ ! sizeof new delete

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

a standard numeric library in C++


compute common mathematical operations and
transformations
declare the functions of:
a) Power
b) Trigonometric
c) Hyperbolic
d) Exponent and logarithm
e) Rounding, abs value & remainder

SJES 2271/ SJEM 2231

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)

DR NOOR FADIYA MOHD NOOR, UM

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)

Exp & Log Functions


Functions
Exponent
Log of base 10
Log of base e
Modf

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)

SJES 2271/ SJEM 2231

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)

DR NOOR FADIYA MOHD NOOR, UM

You might also like