Chapter 3-Part 1
Control structures: Selection
• Algorithm Development (Section 3.1)
• Structured Programming(Section 3.2)
• Conditional Expressions (Section 3.3)
• Selection Statements: if statement
(Section 3.4)
• Selection Statements: switch statement
(Section 3.7)
1
Algorithm Development (Section 3.1)
• Algorithm: a sequence of steps/actions needed to solve a problem.
It is a set of sequential steps/actions that a C++ program should
perform in order to solve the problem.
These steps will eventually correspond to your C++ statements.
How to describe an algorithm
Two techniques:
Pseudo Code: describes algorithm steps using English-like
statements.
Flowchart: describes algorithm steps using a diagram/graphical
notation
2
2
Top-Down Design – Example
Problem: Increase salaries lower than 500 by 30 Omani
Rials and display all salaries!
Pseudo Code Flow Chart
Broad Pseudo Code:
Read salary value Program start/end Or NO
Start main
Increase salary values <500 by 30 symbol
Print salary Input/output
symbol
read salary
Step 2 Needs Refinement: Comparison False
symbol salary<500
Refined Pseudo Code: Computation
True
Read salary value symbol salary=salary+30
If ( salary <500) Input/output
print salary
salary=salary+30 symbol
Print salary Program start/end
symbol Stop main Or Yes
3
3
Structured Programming (Section 3.2)
– Sequence:
• Statements executed in order sequentially, one after
another.
• This is the default structure in C++ programs
– Selection: if, if-else, if-else if, switch
• One set of statements is executed if a given condition is
true, a different set of statements is executed if the
condition is false.
– Repetition: while, do/while, for
• A set of statements is executed repeatedly as long as a
given condition is true.
4
Structured Programming
• Sequence
– Lecture 2
false true
?
• Selection
– Lecture 3 ? => conditional
false expression Or
?
true Comparison
• Repetition
– Lecture 4
5
Conditional Expressions (Section 3.3)
• A conditional expression is a Boolean
expression that evaluates to true or false.
– Example: if (salary <500)
• Relational operators and logical operators
are used to build conditional expressions.
• Selection structures and repetition structures
use conditional expressions.
6
Relational Operators
== equal to
!= not equal to
< less than
> greater than
<= less than or equal
>= greater than or equal
7
Practice – evaluate
Relational Expression Value
1 == 6
5 != 9
5<9
5>10
2<=0
2>=0
8
8
Practice – evaluate
Relational Expression Value
1 == 6 false
5 != 9 true
5<9 true
5>10 false
2<=0 false
2>=0 true
9
9
Logical Operators
! not
&& and
|| or
- can link relational expressions
- create complex conditional expressions
Example
Algebra: 90≤ mark ≤ 100
C++ Statement: mark >= 90 && mark <=100
10
Logical Operators
A B A&&B A||B !A !B
0 0
0 1
1 0
1 1
Truth table for conditional expressions
0 = false
1= true
11
Logical Operators
A B A&&B A||B !A !B
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0
Truth table for conditional expressions
0 = false
1= true
12
Operator Precedence
Precedence Operation Associativity
1 () innermost first
2 ++ -- + - !(type) right to left (unary)
3 * / % left to right
4 + - left to right
5 < <= > >= left to right
6 == != left to right
7 && left to right
8 || left to right
9 = += -= *= /= %= right to left
13
Evaluate Expressions
(-6<0)&&(12>=10)
(3.0 >= 2.0) || (3.0 >= 4.0)
(3.0 >= 2.0) && (3.0 >= 4.0)
! (3.0 >= 4.0)
14
Evaluate Expressions
(-6<0)&&(12>=10)
true && true results in true
(3.0 >= 2.0) || (3.0 >= 4.0)
true || false results in true
(3.0 >= 2.0) && (3.0 >= 4.0)
true && false results in false
! (3.0 >= 4.0)
! false results in true
15
Solve Practice in Page 120
Determine whether the following conditions in problems 1
through 8 are true or false.
Assume that the following objects have been declared and
initialized as shown:
a = 5.5 b = 1.5 k = 3
1. a < 10.0 + k
2. a + b >= 6.5
3. k != a – b
4. b – k > a
5. !(a == 3 * b)
6. -k <= k + 6
7. a < 10 && a > 5
8. fabs(k) > 3 || k < b - a
16
Selection Statements (Section 3.4)
C++ selection statements:
– if statements
• if statement
• if-else statement
• if-else-if statement
– switch statements
– ?: the conditional operator
17
The if statement
one-way branch of action
if(expression)
statement; /*single statement executed
if expression is true */
// statement block is executed if expression is true.
if(expression)
{
statement1;
statement2;
…
statement n;
} 18
The if statement - examples
// example 1: salary will be equal to 330
int salary = 300;
if (salary<500)
salary=salary+30;
// example 2: salary will be equal to 600
int salary = 600;
if (salary<500)
salary=salary+30;
19
// example 3: x will be equal to 2, k will be equal to 1
int x = 4;
int k=0;
if(x>0)
{
x=sqrt(x);
++k;
}
// example 4: x will be equal to -4, k will be equal to 0
int x = -4;
int k=0;
if(x>0)
{
x=sqrt(x);
++k;
}
20
Nested if statement
• If statement (s) within an if statement block
// example 5: x will be equal to 3 and then 7
int x = 10;
int y = 9;
if(x>0)
{
if (y<100)
x=sqrt(y);
x+=4;
}
21
21
Nested if statement
// example 6: x will be equal to 14
int x = 10;
int y = 121;
if(x>0)
{
if (y<100)
x=sqrt(y);
x+=4;
}
22
22
Nested if statement
// example 7: x will be equal to -3
int x = -3;
int y = 121;
if(x>0)
{
if (y<100)
x=sqrt(y);
x+=4;
}
23
23
Nested if statement
// example 8: x will be equal to -3
int x = -3;
int y = 1;
if(x>0)
{
if (y<100)
x=sqrt(y);
x+=4;
}
24
24
Practice : Identify the output of the following programs
25