0% found this document useful (0 votes)
44 views7 pages

PF Practical 02

This document provides an overview of conditional and repetitive control flow in programming. It discusses different types of conditional logic, including one-way, two-way, multi-way, and choice-way selection. It also covers two types of repetitive logic: counter-controlled and sentinel-controlled repetition. The document includes an example problem solving process, showing how to create an IPO chart, algorithm, and flowchart for a problem involving checking multiples of 7 between 1 and a user-input number N.

Uploaded by

Danish Tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views7 pages

PF Practical 02

This document provides an overview of conditional and repetitive control flow in programming. It discusses different types of conditional logic, including one-way, two-way, multi-way, and choice-way selection. It also covers two types of repetitive logic: counter-controlled and sentinel-controlled repetition. The document includes an example problem solving process, showing how to create an IPO chart, algorithm, and flowchart for a problem involving checking multiples of 7 between 1 and a user-input number N.

Uploaded by

Danish Tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical 02

Problem analysis and program design for conditional and repetitive


control flow
Objectives
 Analyzing and designing programs for conditional logic.
 Analyzing and designing programs for repetitive logic.
Tools
 Edraw Max
 Microsoft Word

Keywords: Conditional, repetitive, iterative. Duration: 03 hours

2.1 Introduction

2.1.1 Conditional control flow

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).

2.1.2 Types of conditional control flow

In programming we normally have four types of conditional/selection control flow:

1. One-Way Selection 3. Multi-Way Selection


2. Two-Way Selection 4. Choice-Way Selection

2.1.2.1 One-Way Selection

In one-way selection there is one condition and only one set of statements available.

 If the condition is satisfied (true), the statement set is executed.


 Does nothing when condition is not satisfied (false).

17
Practical 02: Problem analysis and program design for conditional and repetitive control flow

2.1.2.2 Two-Way Selection

In two-way selection there is one condition and two sets of statements available.

 If the condition is satisfied (true), the 1st set of statements is executed.


 If the condition is not satisfied (false), the 2nd set of statements is executed.

2.1.2.3 Multi-Way Selection

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.

2.1.2.4 Choice-Way Selection

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

2.1.3 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.

2.1.4 Types of repetitive control flow

In programming we normally have two types of repetitive/iterative control flow:

1. Counter-Controlled Repetition
2. Sentinel-Controlled Repetition

2.1.4.1 Counter-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).

2.1.4.2 Sentinel-controlled repetition

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

2.2.1 Problem example

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.

Step 01: Create a new blank document in MS Word.


Step 02: Insert a new table of 2 rows 3 columns by going in to Insert Tab>Table
Step 04: Identify all the outputs from the problem statement.
Step 05: Identify all the inputs from the problem statement.
Step 06: Identify all the processing items to be used in solving the problem.
Step 07: Write down the algorithm i.e. the step by step procedure to solve the problem.
Step 08: Fill in all the information in IPO chart.
Input Processing Output
• Number N Processing Items: • Multiples of 7 in
n MOD 7 = 0 between 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

Step 09: Create new basic flow chart by going in to Template


Categories>Flowchart>Basic Flowchart.
Step 10: Click and drag Start or Terminal symbol from toolbox in to
drawing area. Double click the shape to add the text “Start”.
Step 11: Click and drag Data symbol in to drawing area and change
the text to “Input C”.
Step 12: Similarly drag appropriate symbols for each of the
remaining step/instruction.
Step 14: Select Right-Angle Connector from the top ribbon bar.
Step 15: Connect all the symbols to show the flow of program.

20
Practical 02: Problem analysis and program design for conditional and repetitive control flow

EXERCISE
1. Briefly answer the following questions:

a. How conditional logic is different than sequential logic?


b. Can we use multi-way selection if we have two conditions and three statement sets?
c. How can you differentiate multi-way and choice-way selection?
d. How many statement sets are there in multi-way selection if we have 8 conditions?
e. What is advantage of using repetitive logic?
f. When will you use counter-controlled repetition logic?
g. When will you use sentinel-controlled repetition logic?
h. Is it possible to use multi-way logic in place on choice-way logic?

2. For each of the following statement specify which type of conditional or repetition logic
you will use.

A. One-Way Selection E. Counter-Controlled Repetition


B. Two-Way Selection F. Sentinel-Controlled Repetition
C. Multi-Way Selection G. None of them
D. Choice-Way Selection

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

You might also like