0% found this document useful (0 votes)
31 views15 pages

LBPS CHP4

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)
31 views15 pages

LBPS CHP4

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/ 15

Chapter 4

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

The for Loop


The for loop construct provides a compact way of specifying the statements that control the
repetition of the steps within the loop. The for loop consists of the following three parts, each
separated by a semicolon:

Initialization expression: It is the assignment expression that initializes the variable,


which controls the execution of the loop.
Test expression: It is the test expression. When the expression evaluates to false, the
loop terminates.
Increment/decrement expression: It is the expression that increments or decrements
the value of the variable that controls the loop.

The following pseudocode segment depicts syntax of the for loop:

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 pseudocode depicts the solution to the preceding problem:

The following table shows the dry run of the preceding pseudocode.

The Dry Run Table


Example
Consider another example, where you have been assigned the responsibility of generating
the address list of 25 employees working in your office. For each employee, you will need
to accept the name, address, and the telephone number and print the details.

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

The Variables and Their Data Types

The following flowchart represents the solution to the preceding problem.


The following pseudocode segment represents the preceding flowchart:

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 pseudocode segment represents the preceding flowchart:

The following table shows the dry run of the preceding pseudocode/flowchart.
The Dry Run Table

Example

Write a pseudocode to display the average score of 30 students in a school.

The following flowchart represents the solution to the preceding problem.


The following pseudocode represents the preceding flowchart:

</

The while Loop


The while loop executes as long as the condition specified with the loop is true. The moment
the condition becomes false, the loop breaks. The while loop construct can be used for both
fixed and variable loops. This is an iterative construct with a test expression at the top of the
iterative set of statements. The following pseudocode segment shows the syntax of the while
loop:

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

The Variables and Their Data Types

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.

The following flowchart represents the solution to the preceding problem.


The following pseudocode represents the preceding flowchart:
The following table shows the dry run of the preceding pseudocode/flowchart.

The Dry Run Table</

The repeat…until Loop


The repeat…until loop differs from the while loop only in the way the condition is evaluated.
In the while loop, the condition is evaluated or tested before the body of the loop begins.
Only if the condition is true, the control moves to the body of the while loop. In the
repeat…until loop, the body of the loop is executed once, regardless of the condition being
true or false, and then the condition is evaluated. The loop is executed till the condition is
false, and the moment condition becomes true the loop breaks.

The following pseudocode represents the syntax of the repeat…until loop:


Example
Consider an example that calculates the sum of two numbers entered ten times by the user.

The following flowchart represents the solution to the preceding problem.


This following pseudocode represents the preceding flowchart:

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:

A loop is a sequence of instructions that will be repeated more than once.


A loop performs steps in a specified sequence.
There are three types of loops:

for
while
repeat……until

The loops can be of the following two types depending on the number of repetitions:

Fixed loops, where the number of repetitions is known


Variable loops, where the number of repetitions is not known

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.

You might also like