Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

LOOP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Computer Science

Teacher: Maruf Ahmed


Iteration/Repetition/loop: Iteration is when statement/s need to be repeated may be for a fixed number of
times or may be for an unknown number of times. If it is for an unknown number of times, the loop will
continue based on the result of a condition which is set within the structure. There are three different types
of loop structures to handle iteration in pseudocode:

1. FOR … TO …. NEXT
2. WHILE… DO …. ENDWHILE
3. REPEAT … UNTIL

1. FOR… TO … NEXT [known as count-controlled loop]


This repetition statement is used when we know how many times an instruction or set of instructions is to be
repeated.
There are two variations of FOR loop.
Variation 1:
FOR <identifier> ← <value1> TO <value2>
<statements>
NEXT <identifier>

Example of variation 1:
FOR Count ← 1 TO 10
INPUT Num
NEXT Count

In the above example, the loop will continue 10 times. In the start, the Count will have a value of 1 and a
number will be stored in the variable Num, which will be given by a user. In the next time, the value of
Count will get incremented by 1, which means it will become 2 and a new input will be taken. And this way
it will keep incrementing and once the value of Count becomes equal to the end value then the loop will
execute once more and after that the loop will terminate. That means in the above example the Count value
will be 10 after the loop stops execution.

In variation 1, increment of the control variable is always by 1 and value1 must be less than or equal to
value2 for the loop to start. The values before and after TO are both inclusive. This variation is mostly used.

Variation 2:
FOR <identifier> ← <value1> TO <value2> STEP <increment/decrement>
<statements>
NEXT <identifier>

In variation 2, value1 and value2 will both be set and also the value for STEP has to be set. The loop will
continue following the given value after STEP. This value for STEP may be positive or negative. If the
value is positive then value1‟s value must be smaller or equal to value2. If the value after STEP is negative
then value1‟s value must be greater or equal to value2 for the loop to continue.

Example of variation 2:
FOR Count ← 1 TO 10 STEP 2
INPUT Num
NEXT Count
In the above example the loop will continue for the values of Count as follows: 1, 3, 5, 7 and 9. That means
Page 1 of 6
the loop is going to run 5 times not 10 times. When the value of Count will be 11 the loop will stop.

Another example of variation 2:


FOR Count ← 20 TO 1 STEP -2
INPUT Num
NEXT Count

In the above example the loop will continue for the values of Count as follows: 20, 18, 16, 14, 12, 10, 8, 6,
4, and 2. That means the loop is going to run 10 times but not 20 times When the value of Count will be 0
the loop will stop.

Few things to remember about FOR…TO…NEXT loop are:


 There is a variable in FOR … TO … NEXT loop for controlling the number of iterations and is
known as a control variable. The name of the control variable is usually „Count‟ or „Counter‟.
 We specify the initial (lower/higher) and final (higher/lower) values of the control variable in the
opening statement of the loop. These initial and final values are not restricted to the numerical
values only; they can be variables as well.
 If variation1 is followed then the control Variable is automatically incremented by „1‟ each time the
loop iterates. If variation 2 is used then the value after STEP keyword will be used to change the
value for the control variable automatically.
 No extra increment/decrement statement is required within the loop structure to control the counting
of the loop
 End of the loop is indicated by the keyword NEXT <identifier>
 The statements/instructions between FOR and NEXT keywords are repeated

Example: Write pseudocode that will take 10 numbers as input and print their total and average using
FOR…TO…NEXT loop.

Solution:
Total ← 0 [Initializing 0 for the variable Total]
FOR Count ← 1 TO 10 [Variable Count will act as a control variable which will have 1 at the beginning to
start the loop and perform the following statements. Then it becomes 2, 3....10
automatically]
OUTPUT “Enter a number: ”
INPUT Num
Total ← Total + Num [this statement is adding each value of Num with Total and updating Total]
NEXT Count
Avg ← Total/10
OUTPUT “Addition of 10 number is ”, Total
OUTPUT “Average of 10 number is ”, Avg
Few things to Remember:
 Whenever average is to be calculated then totaling must be performed.
 Assign total ← 0 always. As in the above example it is assigned 0.
 FOR Loop must be terminated by the NEXT keyword followed by the control variable

2. WHILE … DO … ENDWHILE [known as pre-condition loop]


This repetition statement is usually used when we do not know how many times an instruction or set of
instructions is to be repeated.
Few things to remember about WHILE…DO…ENDWHILE loop are:
Page 2 of 6
 The loop is repeated as long as the condition is true that is set with WHILE keyword and halted
when the condition is false
 Condition is tested at the beginning of the loop
 The statements within the loop may never be executed if in the first place the condition does not
satisfy.
 The statements/instructions between DO and ENDWHILE keywords are repeated

Example: The following code will add up the values and find the average until 0 is entered

Solution:
Total ← 0, NumCount ← 0
OUTPUT “Enter a number: ”
INPUT Num
WHILE Num < > 0 DO [for checking the condition that depends on the value of a variable, the value
must be taken as input or it must have a value assigned in it before the WHILE loop,
and the other values for that variable must be taken as input inside the loop to
continue the loop]
Total ← Total + Num [This statement has to be before the next input of Num otherwise the first value
will be lost]
NumCount ← NumCount + 1 [This NumCount variable is used to count the number of values that
has been entered other than 0. This will be needed for finding the average.
This statement should be before the next input taken]
OUTPUT “Enter a number: ”
INPUT Num [This Num variable will continue to accept values within the loop as long as the value is
not given 0]
ENDWHILE
Avg ← Total / NumCount
OUTPUT “The average of ”, NumCount, “ number is ”, Avg

WHILE loop may also be used in a place where fixed number of iterations is required

Example: Write pseudocode that will take 10 numbers as input and print their total and average using
WHILE…DO…ENDWHILE loop.
Solution:
Total ← 0, Count ← 1 [When Count ← 0, then in the condition part it will be Count < 10 or Count <=9]
WHILE Count <= 10 DO [Count < 11 can also be written in the condition part]
OUTPUT “Enter a number: ”
INPUT Num
Total ← Total + Num
Count ← Count + 1 [this is for the increment of the Counter so that every time it is increased by 1]
ENDWHILE
Avg ← Total / 10 [this can also be written as Avg ← Total/Count-1. Here -1 because Count variable has
started from 1 and will stop working when it becomes 11. But if it had started from 0 then
this statement would be Total/Count since the loop would terminate when the value of
Count would be 10]
OUTPUT “Total of 10 number is ”, Total, “ and Average of 10 number is ”, Avg

Page 3 of 6
3. REPEAT.…UNTIL [known as post-condition loop]
It is a repetition statement that is usually used when we do not know how many times an instruction or set
of instructions is to be repeated.
 This loop will be repeated as long as the condition is false that is set with UNTIL keyword and it
will stop executing once the condition is true.
 The condition is tested at the end of the loop
 The loop will always execute at least once

Example: the values will be totaled until the value 100 is entered. The last input should not be used to find
the total

Solution:
Total ← 0
REPEAT
OUTPUT “Enter a number: ”
INPUT Num
IF Num < > 100
THEN
Total ← Total + Num
ENDIF
UNTIL Num = 100
OUTPUT “Addition is ”, Total

REPEAT loop may also be used in a place where fixed number of iterations is required
Example: Write pseudocode that will take 10 numbers as input and print their total and average by using
REPEAT…UNTIL loop.
Solution:
Total ← 0, Count ← 1
REPEAT
OUTPUT “Enter a number: ”
INPUT Num
Total ← Total + Num
Count ← Count + 1 [this is for the increment of the Counter so that every time it is increased by 1]
UNTIL Count = 11 [this can also be written as UNTIL Count > 10 or UNTIL Count>=11]
Avg ← Total/10 [this can also be written as Avg ← Total/Count-1 since the last value of Count was 11 that
is why -1 is needed here]
OUTPUT “Total of 10 number is ”, Total, “ and Average of 10 number is ”, Avg

NESTED LOOP:
In nested loop, for each iteration of the outer loop, the inner loop will complete its full cycle. After the
completion of entire cycle of inner loop, it will go back to the outer loop.

Example: 100 positive numbers will be totaled. In the process if any negative or zero values are given then
those should not be accepted by the program and should not be counted as a number. [This is an example of
nested loop where one loop is inside another loop]

Page 4 of 6
Solution:
Total ← 0
FOR Count ← 1 TO 100
REPEAT [starting of a new loop inside the FOR loop]
OUTPUT “Enter a number: ”
INPUT Num
UNTIL Num > 0 [End of the inner loop. This will ensure that only a positive value will come out of
the loop otherwise it will continue asking for a number. This is an example of
validation also]
Total ← Total + Num [The positive value that came out from REPEAT..UNTIL loop will be added with
Total]
NEXT Count
OUTPUT “Total of 100 positive number is ”, Total

Example: Finding the highest and lowest from 100 numbers

First example is by initializing a small value for highest and a large value for lowest
Solution:
Highest ← -100000, Lowest ← 100000 [Always initialize a small value for Highest and a large value for
Lowest]
FOR Count ← 1 TO 100
OUTPUT “Enter a number: ”
INPUT Num
IF Num > Highest
THEN
Highest ← Num [Value of Num has been copied to Highest if the condition is true]
ENDIF
IF Num < Lowest
THEN
Lowest ← Num [Value of Num has been copied to Lowest if the condition is true]
ENDIF
NEXT Count
OUTPUT Highest, “ is the highest ”, “ and ”, Lowest, “ is the lowest”

N.B. Highest and Lowest should be in separate IF as they are two different checking

Note that, the above program will not work in the following two situations:
 Highest will not give correct result if all the values are less than -100000 being entered.
 Lowest will not give correct result if all the values are greater than 100000 being entered

Second example is by initializing the first number that has been entered to both the highest and the lowest

Solution:
OUTPUT “Enter a number: ”
INPUT Number
Highest ← Number [the first value will be stored in Highest]
Lowest ← Number [the first value will also be stored in Lowest]
FOR Count ← 2 TO 100 [Count is starting from 2 because first entry is already taken above]
Page 5 of 6
OUTPUT “Enter a number: ”
INPUT Number [This has to be the same variable Number which was taken outside the loop also]
IF Number > Highest
THEN
Highest ← Number [Value of Number has been copied to Highest if the condition is true]
ENDIF
IF Number < Lowest
THEN
Lowest ← Number [Value of Number has been copied to Lowest if the condition is true]
ENDIF
NEXT Count
OUTPUT “The highest is ”, Highest, “ and the lowest is ”, Lowest

Write a pseudocode which will take input of 100 numbers and find out how many of those are positive, how
many are negative and how many are zero.

Solution:
PosCount ← 0, NegCount ← 0, ZeroCount ← 0
FOR Count ← 1 TO 100
OUTPUT “Enter a number: ”
INPUT Num
IF Num > 0
THEN
PosCount ← PosCount + 1
ELSE
IF Num < 0
THEN
NegCount ← NegCount + 1
ELSE
ZeroCount ← ZeroCount + 1
ENDIF
ENDIF
NEXT Count
OUTPUT PosNum, “ positive numbers are there”
OUTPUT NegNum, “ negative numbers are there”
OUTPUT ZeroNum, “ zeros are there”

Write a pseudocode that will ask the user of how many numbers the user wants to input and find the
total of those numbers.

Total ← 0
OUTPUT “How many numbers do you want to input? ”
INPUT Value
FOR Count ← 1 TO Value [The loop will continue up to whatever input has been stored in the variable
Value]
OUTPUT “Enter a number: ”
INPUT Num
Total ← Total + Num
NEXT Count
OUTPUT “Total of ”, Value, “ number is ”, Total

Page 6 of 6

You might also like