DFC20113 - CHAPTER 3 (PROGRAM CONTROL STRUCTURES)
DFC20113 - CHAPTER 3 (PROGRAM CONTROL STRUCTURES)
Page 03
Introduction
A program is usually not limited to a linear sequence of
instructions.
During its process it may bifurcate, repeat code or take
decisions.
For that purpose, C++ provides control structures that
serve to specify what has to be done by our program, when
and under which situations.
Program control typically involves executing particular code
based on the outcome of a prior operation or a user input.
FLOWCHART FOR TYPES OF CONTROL
STRUCTURE
Page 05
LEARNING OUTCOMES
at the end of this sub-chapter, students should be able
to: Identify the selection control structure statement:
‘if’ statement
‘switch’ statement
Explain the ‘if’ statement type:
Simple if statement
Else statement
3.2 Else if statement
Solve problem using Write an algorithm for each ‘if’ statement type and code into a program based on
a problem statement
selection control Differentiate the assignment (=) and equality (==) operator.
structures Apply the nested if.
Describe the switch case statement
Identify the syntax for switch case statement
Describe the uses of break statement
Convert the nested if statement to switch
Page 03
Solve a given problem by writing algorithm, convert into a program, run, test and
debug using selection statement.
Selection Control Structure
If Switch
Single if
Types of
Selection Control Structure
if-else
Nested if Page 07
Selection Control Structure
Used to execute a set of statements
when the given condition is satisfied.
Conditional statements within the
block are executed when the condition The if statements written within the
in the if statement is satisfied. body of another if statement to test
multiple conditions is called nested if.
Page 09
Page 10
Page 10
Page 10
Switch Statement
Is a multi-way selection statement and contains various case
statements.
The case statements are executed based on the value of the
expression.
Page 05
Switch Statement
(Expression)
Data type must be INTEGER
or
CHAR only
Page 08
Page 10
BREAK Statement
Using break we can leave a loop even if the condition
for its end is not fulfilled.
It can be used to end an infinite loop, or to force it to
end before its natural
In looping, it causes the //code (next slide) to be skipped
and terminates the loop.
In switch case statements, break causes the remaining
cases to be skipped--it prevents "falling through" to
other cases.
Page 09
In the program code, the while loop will run,
as long i is smaller then 20. In the while
loop there is an if statement that states
that if i equals 10 the while loop must stop
(break). The result is that only 10 Hello will Page 10
be printed.
switch if-else Page 10
Programming Exercise
1. Based on the program given, convert it to switch-case statement.
Page 08
Programming Exercise
Page 06
Structure of loops statement
Loop condition
Page 07
Structure of loops statement
condition tested
Loop condition
first
latest
Types of loops
From one number to another
number and increases by a specified
value each time Loops are useful for things
that want to loop at least
once.
Example:
for (i=0; i<5; i++)
{
cout << “\n” << i ;
}
Page 09
for loops statement
Program code Loop condition
Page 09
Structure of loops statement
while
Loop condition
do-while Page 07
while loops statement
Syntax: Example:
initialization; int i = 0;
while (condition) while (i<5)
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
} }
Page 09
while loops statement
Page 09
do-while loops statement
Syntax: Example:
initialization; int i = 0;
do do
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
} while (i<5);
}while (condition);
Page 09
do-while loops statement
Page 09
Nested for loops statement
Page 06
Nested for loops statement
Page 09
Differences between while and do..while
statements
Page 09
Control transfer statement
Program code
Loop condition
continue
The conti
nue statem
provides a ent
convenient
jump to th way to
e next of
body for t the loop
he current
•It is some iteration.
times nece
skip some ssary to
statement
/s inside
the loop.
Page 07
Control transfer statement
Program code
Loop condition
exit()
The exit(
) function
a program also ends
before its
terminatio normal
n. It requi
Standard res the
Library he
stdlib.h. T ad er file,
he format
is:
exit(value
where val );
ue is an in
teger
variable o
r value. Page 07
Differences between BREAK, CONTINUE and EXIT() statements
Page 09
break statement
Page 09
continue statement
Page 09
exit() statement
Page 09
Programming Exercise
3. Write a program in C++ to get input one number from user. The
program will repeat until user entered 0. Below is the example output:
Page 08
THANK
YOU
Page 15