Dfc20303 - Chapter 3 (Program Control Structure)
Dfc20303 - Chapter 3 (Program Control Structure)
PROGRAMMING
FUNDAMENTALS
CHAPTER 3
PROGRAM CONTROL STRUCTURE
01 05
COURSE LEARNING PROGRAMMING
OUTCOME EXERCISES
TABLE OF 02
TYPES OF PROGRAM
CONTENT
CONTROL STRUCTURE
03 SELECTION CONTROL
STRUCTURES
04
REPETITION CONTROL
STRUCTURES
COURSE LEARNING
OUTCOMES
CLO 1:
Apply programming elements and articulate how they are
used to achieve a working program. (C3, PLO2)
CLO 2:
Demonstrate appropriate algorithm based on well-defined
problem using C++ programming language. (P3, PLO3)
02
TYPES OF CONTROL
STRUCTURE
LEARNING OUTCOMES
3.1 Understand program control structure
Describe control structure
Describe the approaches can be chosen depending on problem statement:
a) Sequential
b) Selection
c) Iterational (Repetition)
TYPES OF PROGRAM CONTROL STRUCTURE
INTRODUCTION
A program is usually not limited to a linear sequence of instructions.
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.
SELECTION
This control structure enables a program to choose between two or more paths of execution based on
specific conditions. Selection structures include conditional statements such as if-else and switch-case,
which use Boolean expressions to evaluate the conditions and execute the appropriate block of code.
REPETITION
This control structure allows certain blocks of code to be executed repeatedly as long as a
condition remains true. Iteration structures include loops like the for loop, while loop, and do-while
loop.
FLOWCHART FOR TYPES OF CONTROL STRUCTURE
Repetition
Sequence Selection
03
SELECTION CONTROL
STRUCTURES
LEARNING OUTCOMES
3.2 Solve problem using selection control structures
Identify the selection control structure statement:
a. if statement
b. switch case statement
Explain the ‘if’ statement type:
a. if statement
b. switch case statement
Identify the syntax for each ‘if’ statement type
Write program for each ‘if’ statement type based on a problem statement
Describe the switch case statement
Identify the syntax for switch case statement
Describe the use of break statement
Write program using switch case statement.
Write program to convert ‘if’ statement to switch case statement vice versa.
SELECTION CONTROL STRUCTURE
DEFINITION
It allows instructions to be executed in a non- sequential ways.
The user can choose what he want to process. It compares two expressions.
Based on the comparison, it takes a certain course of action.
Types of selection
IF STATEMENTS
Used to execute a set of statements when the
given condition is satisfied. Conditional
Nested if
statements within the block are executed when The if statements written within the body of
the condition in the if statement is satisfied. another if statement to test multiple
conditions is called nested if.
Single if
Executes the set of statements in if block, when
the given condition is satisfied.
Executes the statements in the else block, when
the condition is not satisfied.
if-else
IF STATEMENTS SYNTAX
SINGLE IF
IF ELSE
NESTED IF
SWITCH STATEMENTS
DEFINITION
Is a multi-way selection
statement and contains various
case statements.
The case statements are
executed based on the value of
the expression.
SWITCH STATEMENTS
(Expression)
Data type must be INTEGER
or
CHAR only
SWITCH STATEMENTS
BREAK STATEMENTS
DEFINITION
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 switch case statements, break causes the remaining cases to be skipped and it prevents
"falling through" to other cases.
BREAK STATEMENTS
condition tested
latest
REPETITION CONTROL STRUCTURE
From one number to another number do while
and increases by a specified value
each time
Loops are useful for things that want to loop
at least once.
for
The while loop can be used if we
don’t know how many times a loop
must run.
while
FOR LOOP STATEMENT
Syntax:
for(initialization; condition; increment/decrement)
{
statement;
}
Example:
for (i=0; i<5; i++)
{
cout << “\n” << i ;
}
FOR LOOP STATEMENT
Program Code
Flow Chart
WHILE LOOP STATEMENT
Syntax: Example:
initialization; int i = 0;
while (condition) while (i<5)
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
}
}
WHILE LOOP STATEMENT
Program Code
Flow Chart
DO WHILE LOOP STATEMENT
Syntax: Example:
initialization; int i = 0;
do do
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
}while (i<5);
}while (condition);
DO WHILE LOOP STATEMENT
Program Code
Flow Chart
NESTED FOR LOOP STATEMENT
break exit()
continue
05
PROGRAMMING
EXERCISE
PROGRAMMING EXERCISES
1. Write a program that prints the even number between number 1 to 20 by using for loops control
structures.
2. Write a program that enables user to input 5 hobbies by using while loops control structures.
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:
THANK YOU