
Frequently used data types
1
•float
and
double
data types for storing real numbers.
• The
float
data type has a precision of seven digits
-This means after the decimal point you can have seven digits
Example:
-3.14159 534.322344
0.1234567
•
The
double
data type has a precision of fifteen digits
-This means after the decimal point you can have fifteen digits
Example :
-
3738.7878787878 3.141592653589790
0.123456789123456

LECTURE # 5 :
STRUCTURED PROGRAMMING
C++ Operators
D. Abdalrahman R. Qubaa
First Class, System and Control Engineering Dep.

Content
3
Arithmetic Operations
Arithmetic Operators
Increment and decrement operators
Decision making operators (Equality, relational and
logical)
Conditional operator
Precedence and associativity of operators
Common errors

Arithmetic Operations
Operators: Data connectors within expression or equation
Operators types based on their mission
1. Arithmetic
: addition + , subtraction -, modulo division / , ...etc (x+y)
2. Equality and Relational
: equal to == , less than <, grater than >,
…etc
(x<y)
3. Logical :
NOT, AND, OR (x and y)
4

Operators
Operators types based on number of operands
Unary operators
◼
Have only one operand
◼
May be prefix or postfix
◼
e.g.
++ !
--
(
x
++ ,
y
--)
Binary operators
◼
Have two operands
◼
Infix
◼
e.g.
+
&&
==
-
(
x
&&
y
,
x
+
y
)
Ternary operators
◼
Have three operands
◼
e.g. ? : (
i > j
?
max=i
:
max=j
)
5

Assignment statement
Assignment statement takes the form below
Expression is evaluated and its value is assigned to the variable on
the left side
Shorthand notation
varName = varName
operator
expression;
varName
operator
= expression;
6
varName = expression;
c = c + 3;
c += 3;

Arithmetic Operators
Arithmetic Operators: All of them are binary operators
Arithmetic expressions appear in straight-line form
Parentheses () are used to maintain priority of manipulation
7
C++ operation
C++ arithmetic
operator
Algebraic
expression
C++ expression
Addition
+
f + 7
f + 7
Subtraction
-
p - c
p - c
MUltiplication
*
bm or b . m
b * m
Division
/
x / y or x ÷ y
x / y
Modulus
%
r mod s
r % s

Assignment between objects
Assignment between objects of
the same type
is always supported
8
Assignment
operator
Sample
expression
Explnation
Assigns
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
+=
c += 7
c = c + 7
10 to c
-=
d -= 4
d = d – 4
1 to d
*=
e *= 5
e = e * 5
20 to e
/=
f /= 3
f = f / 3
2 to f
%=
g %= 9
g = g % 9
3 to g

Decision Making:
Equality
and
Relational Operators
9
Standard algebraic
equality operator or
relational operator
C++ equality
or relational
operator
Example
of C++
condition
Meaning of
C++ condition
Relational operators
>
>
x > y
x
is greater than y
<
<
x < y
x
is less than y
>=
x >= y
x
is greater than or equal to y
<=
x <= y
x
is less than or equal to y
Equality operators
=
==
x == y
x
is equal to y
!=
x != y
x
is not equal to y

Arithmetic Operators Precedence
Operators in parentheses evaluated first
Nested/embedded parentheses (Operators in innermost pair first)
Multiplication, division, modulus applied next
Addition, subtraction applied last
10
(Operators applied from left to
right)
Operator(s)
Operation(s)
Order of evaluation (precedence)
()
Parentheses
Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or %
Multiplication Division
Modulus
Evaluated second. If there are several, they re
evaluated left to right.
+ or -
Addition
Subtraction
Evaluated last. If there are several, they are
evaluated left to right.

Example
The statement is written in algebra as
z = pr % q + w / (x – y)
How can we write and evaluate the previous statement in C++ ?
z = p * r % q + w / (x -
y);
11
5
3
4
2
1
6

Example:

Exercise 1
State the order of evaluation of the operators in each of the
following C++ statements and show the value of
x
after
each statement is performed.
x = 7 + 3 * 6 / 2 - 1;
x = 2 % 2 + 2 * 2 - 2 / 2;
x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
15
3
324

Increment and Decrement Operators
Unary operators
Adding 1 to or (subtracting 1 from) variable
’s value
Increment operator gives the same result of
(c=c+1) or (c+=1)
Decrement operator gives the same result of
(c=c-1) or (c-=1)
14
Operator Called
Sample
expression
Explanation
++
Preincrement
++a
Increment a by 1, then use the new value of
a in the expression in which a resides.
++
Postincrement
a++
Use the current value of a in the expression
in which a resides, then increment a by 1.
−−
Pridecrement
--b
Decrement b by 1, then use the new value of
b in the expression in which b resides.
−−
Postdecrement
b--
Use the current value of b in the expression
in which b resides, decrement b by 1.

Examples
15
int
x = 10;
cout << “x = “ << ++x << endl;
cout << “x = “ << x << endl;
Example # 1
x = 11
x = 11
output # 1
int
x = 10;
cout << “x = “ << x++ << endl;
cout << “x = “ << x << endl;
Example # 2
x = 10
x = 11
output # 2

Examples
16
int
x = 10 , y;
y = ++x;
cout << “x = “ << x << endl;
cout << “y = “ << y << endl;
Example # 1
x = 11
y = 11
output # 1
int
x = 10 , y;
y = x++;
cout << “x = “ << x << endl;
cout << “y = “ << y << endl;
Example # 2
x = 11
y = 10
output # 2

Relational and Equality Operators
Binary operators
Used in decision -making statements
17
Standard algebraic
equality operator or
relational operator
C++ equality
or relational
operator
Example
of C++
condition
Meaning of
C++ condition
Relational operators
>
>
x > y
x
is greater than y
<
<
x < y
x
is less than y
>=
x >= y
x
is greater than or equal to y
<=
x <= y
x
is less than or equal to y
Equality operators
=
==
x == y
x
is equal to y
!=
x != y
x
is not equal to y

Relational and Equality Operators (cont.)
Have the same level of precedence
Applied from left to right
Used with conditions
Return the value true or false
Used only with a single condition
18

Logical Operators
Used to combine between multiple conditions
&& (logical
AND
)
true if both conditions are true
gender == 1 && age >= 65
|| (logical
OR
)
true if either of condition is true
semester_Average >= 90 || final_Exam >= 90
19
1
st
condition
2
nd
condition

Logical Operators (cont.)
!
(logical
NOT
, logical negation)
Returns
true when its condition is false, and vice versa
!( grade == sentinelValue )
Also can be written as
grade != sentinelValue
20

Conditional operator (
?:
)
Ternary operator requires three operands
Condition
Value when condition is
true
Value when condition is
false
Syntax
21
Condition
?
condition’s true value
:
condition’s false value

Examples
Can be written as
Can be written as .. ?
22
grade >= 60 ? cout<<“Passed” : cout<<“Failed”;
Example # 1
cout << (grade >= 60 ? “Passed” : “Failed”);
int
i = 1, j = 2, Max;
Max = ( i > j ? i : j );
Example # 2

bool
Variables in Expressions
false
is zero and
true
is any non-zero
The following codes applies implicit conversion between bool and
int
23
int
x = -10 ;
bool
flag = x ;
//
(true)
int
a = flag ;
// assign the value 1
int
b = !flag;
// assign the value 0
x = flag + 3;
// assign the value 4
cout << a << b << x;
On the screen: 1 0 4
Code # 1

Common Compilation Errors
Attempt to use % with non-integer operands
Spaces between pair of symbols e.g. (= =, ! =,
…etc)
Reversing order of pair of symbols e.g. =!
Confusing between equality (==) and assignment operator (=)
24

Exercise - 1
25
What is the output of the following program?
1
#include
<iostream>
2
3
4
5
6
int
main()
7 {
8
int
x;
int
y;
int
z;
9
10 x = 30; y = 2; z = 0;
11
12 cout << (++++x && z ) << endl;
13 cout << x * y + 9 / 3 << endl;
14 cout << x << y << z++ << endl;
15
16
return
0;
17
18 }
// end main
What is wrong with the following
program?
1
int
main()
2 {
3
int
a,b,c,sum;
4
sum = a + b + c ;
5
return
0;
6 }
Exercise - 2