PF Practical 02
PF Practical 02
2.1 Introduction
Conditional control flow also referred to as conditional logic or selection logic is one of the order in
which the program instructions are executed on the basis of one or more conditions. If the conditions
are satisfied the instructions are executed else they will not be executed (skipped).
In one-way selection there is one condition and only one set of statements available.
17
Practical 02: Problem analysis and program design for conditional and repetitive control flow
In two-way selection there is one condition and two sets of statements available.
Multi-way selection is series of two-way selections. In multi-way selection there are multiple
conditions and multiple sets of statements available, we choose any one of them.
Choice-way selection is the simplest form of multi-way selection. Here you are provided with multiple
options and given a choice to select only one of the option among them. Every option is associated
with different statement set.
18
Practical 02: Problem analysis and program design for conditional and repetitive control flow
Repetitive control flow also referred to as iterative logic or loop, is one of the order in which the
program instructions are executed repetitively multiple number of times.
1. Counter-Controlled Repetition
2. Sentinel-Controlled Repetition
The case, when we know exactly how many times we have to repeat
the statements’ execution, we will use counter-controlled
repetition logic. Suppose the set of statements need to be executed
is N number of times. We will first set the counter to 1, and every
time we check the counter (counter <= N) and increment it (counter
= counter + 1).
The case, when we do not know exactly how many times we have
to repeat the statements execution, we will use sentinel-controlled repetition logic. Suppose we have
to create a program that continuously reads lines from a text file and displays them until it reaches
the end of the file. In this case we do not know how many lines will be there in different text files. It
is an example of sentinel-controlled repetition.
We do not always know how times we need to repeat, but we may know that the last entry is a special
value, called a sentinel. The sentinel-controlled repetition is also known as condition-controlled
repetition.
19
Practical 02: Problem analysis and program design for conditional and repetitive control flow
2.2 Procedure
Considering the following problem statement, we are creating its IPO chart, algorithm and flow chart.
Problem statement: Write a program that receives the integer number N from the user and displays
all the multiples of 7 from 1 to N.
Algorithm:
Step 01: Start
Step 02: Input number N from the user
Step 03: Set i = 1
Step 04: Repeat Step 05 to Step 07 while i <= N
Step 05: If (i MOD 7 = 0) then GOTO Step 06 else GOTO Step 07
Step 06: Print i
Step 07: i = i + 1
Step 08: End
20
Practical 02: Problem analysis and program design for conditional and repetitive control flow
EXERCISE
1. Briefly answer the following questions:
2. For each of the following statement specify which type of conditional or repetition logic
you will use.
Statement Logic
Specifying grade on the basis of percentage
Checking number is even or odd
Getting three numbers as input from the user
Calculating the area of triangle
Checking number is positive or not
Checking number is prime or composite
3. For each of the following problem statement, create the IPO chart with algorithm and flow
chart.
Problem Statement 1
Write a computer program that asks the user to enter three angles of a triangle. The program displays
whether the triangle is right-angle, acute-angle or obtuse-angle.
Problem Statement 2
Write a computer program that displays the sum of first 10 odd multiples of 3.
Problem Statement 3
Write a computer program that displays the sum of last 5 four digit multiples of 5.
21
Practical 02: Problem analysis and program design for conditional and repetitive control flow
22
Practical 02: Problem analysis and program design for conditional and repetitive control flow
23