LBPS CHP4
LBPS CHP4
Understanding Iterations
Programs become lengthy not only because of complex decision-making constructs, but also
because they are designed to perform tasks that are repetitive in nature. To reduce the length
of code, in such a situation, iterative constructs can be used.
This chapter explains the concept of iterative programming and checks its functionality by
using the dry run table.
Objectives
In this chapter, you will learn about:
Identifying repetitive processes
Iteration
One of the most important characteristics of a computer is its ability to execute a series of
instructions repeatedly. This gives you the flexibility to control the number of times a task
is to be repeated.
Let us take an example where you have to accept 10 numbers, find their sum, and display
the result. To solve this problem, you will declare 10 variables and then find their sum. What
happens if you have to accept 50 numbers and find their sum?
Here, the task of accepting the number is repeated. To solve such problems, you can use the
concept of iteration/loops where a set of tasks are performed repeatedly. A loop is a sequence
of instructions that is repeated more than once. A loop must always perform certain steps in
a specified sequence. There are two types of loops, fixed loop where the number of
repetitions is known and variable loop where the number of repetitions is not known.
The following types of loops can be used in pseudocode/flowchart to perform the repeated
tasks:
for
while
repeat…until
Example
Consider an example that accepts 5 numbers and checks whether the number is even or odd.
If the number is even, display the message, ‘The Number Is Even’. Otherwise, display the
message, ‘The Number Is Odd’.
The following table shows the dry run of the preceding pseudocode.
The following table shows the variables and their data types used in the
pseudocode/flowchart.
Variable Data Type Variable Name
Employee Name character cName
Employee Address character cAddress
Telephone Number character cTelno
Counter numeric nCounter
Example
Consider an example that accepts 10 numbers and displays the sum of these numbers.
The following flowchart represents the solution to the preceding problem.
In the given flowchart, the variable, nCounter, is used to control the loop. This variable is
used to control the number of iterations of the loop. The counter variable is to be initialized
before it can be used. In this example, it is initialized with a value zero. The decision box
checks if the condition is true or false. If the condition is true, the input is received from the
user, added to the variable, nSum, and the counter is incremented by 1. The control loops
back to the decision box. The decision box evaluates the condition and the loop is repeated
if the condition is true.
The following table shows the dry run of the preceding pseudocode/flowchart.
The Dry Run Table
Example
</
Example
Consider an example that accepts 5 numbers and checks whether the number is even or odd.
If the number is even, display the message, ‘The Number Is Even’. Otherwise, display the
message, ‘The Number Is Odd’.
You used the for loop to solve the preceding problem. The same problem can also be solved
by using the while loop, as shown in the following pseudocode:
Example
Consider the example for automated telephone call transfer to various departments of the
company, such as Marketing, Finance, Customer Care, Human Resource (HR), and
Information.
The solution to the preceding problem given in the previous chapter would work only once.
Suppose you want that the user should be given a choice to continue with the other
departments or exit depending upon whether he/she wants to make further queries. For this,
you need to implement the while loop along with the switch…case construct, as shown in the
following pseudocode:
Example
Consider an example where you have been assigned the task of preparing the test
performance report for the candidates appearing for an interview. All the candidates have to
take three tests. The individual score in each test has to be greater than 75 and the average
score across the three tests should be a minimum of 80. A candidate is selected based on the
preceding criteria. In addition, a call letter needs to be sent to candidates who have been
selected and a rejection letter is to be sent to the rest.
Represent the logic for the preceding process using pseudocode and a flowchart. Apart from
finding out whether a candidate has to be sent a call letter or a rejection letter, calculate the
number of candidates who have been sent interview call letters and the number of candidates
who have been sent rejection letters, using a flowchart.
We have to repeat the set of tasks for all the candidates. However, we do not know the exact
number of candidates whose details have to be accepted. Therefore, we need to have a
method by which we can keep asking the user if there is any more data to be entered. We
can do this by having a variable that stores the user’s choice. By checking the value in this
loop variable, we can decide whether the iteration has to continue or not.
To solve the given problem, we need a variable, which will store the choice of the user that
decided whether to continue the loop. Therefore, a variable, cChoice, of type character, is
declared and initialized to ‘Y’ as it is important that the condition should be true, so that the
control enters the loop at least once.
The following table displays the variables and their data types used in the
pseudocode/flowchart.
Label Data Type Variable Name
Candidate code character cCode
Candidate name character cName
Test 1 score numeric nTest1
Test 2 score numeric nTest2
Test 3 score numeric nTest3
Average score numeric nAverage
Loop choice character cChoice
Total selected numeric nTotSelect
Total rejected numeric nTotReject
Since, the exact number of iterations is not known, the loop variable, cChoice, is initialized
to a character value ‘Y’. As long as the value of the variable cChoice is either ‘Y’ or ‘y’, the
loop continues, else the loop terminates.
In order to calculate the total number of call letters and rejection letters that is sent, you need
to store the number of candidates selected or rejected. These values can be stored in the
variables, nTotReject and nTotSelect. These variables are initialized with the value 0. During
the execution of the loop, the value in this variable keeps increasing and at the end of the
loop, we arrive at the summed up value.
Exercises
Exercise 1
Draw a flowchart and write the pseudocode to display the highest of any 10 numbers entered.
Exercise 2
Draw a flowchart and write the pseudocode to print the product of the first 10 even numbers.
Exercise 3
Draw a flowchart and write the pseudocode to accept 5 numbers and display the total number
of odd and even numbers.
Summary
In this chapter, you learned that:
for
while
repeat……until
The loops can be of the following two types depending on the number of repetitions:
The for loop is used when the number of iterations of the loop is known before the
control enters the loop.
The while loop executes till the condition is true, and the moment condition becomes
false the loop breaks.
In the repeat…until loop, the body of the loop is executed at least once, regardless of
the condition being true or false.