0% found this document useful (0 votes)
37 views9 pages

Looping

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)
37 views9 pages

Looping

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

Looping

Looping or repetition is a way to Repeat certain instructions either a


set of instructions for number of times depending upon a particular
condition.
There are two constructs for looping;
1) FOR‐TO‐NEXT Loop (used for Counting based looping)
2) WHILE & REPEAT‐UNTIL (Used for Condition based looping)
) FOR‐TO‐NEXT Loop (used for Counting based looping)
A variable is set up with a start value and an end value and then
incremented in steps of one until the end value is reached and the
iteration finishes.
When some statements of algorithm need repeating, then these
statements are called ‘iteration

EXAMPLE: Write pseudocode without declaring variable that will take


amount of number as input and calculate average of amount of that
numbers. Print their average.
EXAMPLE: Write pseudocode that will take 15 numbers as input and
print their average

Example: Take 10 numbers as input and output largest number.


Pseudocode:
DECLARE BiggestSoFar, NextNumber , Count : Integer
INPUT “ Enter Number ” BiggestSoFar
FOR Count ← 1 TO 9
INPUT “Enter Next Number ” NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
Next
OUTPUT BiggestSoFar

REPEAT ... UNTIL ( Post‐Condition loop


This loop is used when number of repetitions is not known and actions
are repeated UNTIL
given condition becomes true. Actions in this loop are always
completed at least once

Example: Take 10 numbers as input and output largest number using


REPEAT…..UNTIL.
DECLARE BiggestSoFar, Counter , NextNumber : Integer
INPUT BiggestSoFar
Counter ←1
REPEAT
INPUT “ Enter Number ” NextNumber
Counter ← Counter + 1
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
UNTIL Counter= 10
OUTPUT BiggestSoFar
Example: A sequence of non‐zero numbers is terminated by 0. Take this
sequence as input and output the largest number.
Pseudocode:
INPUT BiggestSoFar
REPEAT
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar NextNumber
ENDIF
UNTIL NextNumber = 0
OUTPUT BiggestSoFar
While Do EndWhile Loop ( Pre‐Condition loop )
This repetition statement is used when we don’t know how many times
an instruction or set of instructions is to be repeated. This is a
conditional loop, which has a test at the start and repeats until
condition is false:
Things to remember about WHILE…DO…ENDWHILE loop are:
❖ Condition is tested at the beginning of the
loop.
❖ The loop is repeated until a condition is
true and halted
when the condition is false.
❖ The statements between DO and
ENDWHILE keywords are repeated.
Example: Write pseudocode that will take numbers input and add
them while input number is greater than 0. Print final result.
DECLARE Total , num : Integer
Total ← 0
num ← 1
WHILE num > 0
DO
PRINT “ Please Input Number greater than zero”
INPUT num
Total ←Total + num
ENDWHILE
PRINT “Total sum is:”, Total
Example: Write pseudocode that will take numbers input and add them
while input number is greater than 0. Print final result.
DECLARE Total , num : Integer
Total ← 0
num ← 1
WHILE num > 0
DO
PRINT “ Please Input Number greater than zero”
INPUT num
Total ←Total + num
ENDWHILE
PRINT “Total sum is:”, Total
Example: Write pseudocode that will take numbers input, add them and
calculates average while input number is greater than or equal to 0. Print
average of all input numbers.

DECLARE Total, Count , num : Integer


DECLARE avg : Real
Total ← 0
PRINT “ Enter Number to Add”
INPUT num
Count ← 1
WHILE num >= 0
DO
Total ← Total + num
INPUT num
Count ←Count + 1
ENDWHILE
avg ← Total/Count
PRINT “The Average is:”, avg
Home Task: Calculating running totals and averages . Take 10 numbers as
input and output the sum of these numbers and the average. DECLARE
Total , Count , Num , Average : Integer
Total ←O
FOR Count ← 1 TO 1 0
INPUT “Enter number ” Num
Total ← Total + Num
Next Count
OUTPUT Total
Average←Total / 10
OUTPUT Average
It is a type of construct in which one loop contain another loop. Each
time round the outer loop we complete the inner loop. Take as input
two numbers and a symbol. Output a grid made up entirely of the
chosen symbol, with the number of rows matching the first number
input and the number of columns matching the second number
input.
For example the three input values 3, 7 and&, result in the output:
&&&&&&&
&&&&&&&
&&&&&&&
DECLARE NumberOfRows, NumberOfColumns , ColumnCount ,
RowCount : Integer
DECLARE Symbol : Char
INPUT “Enter number of Row” NumberOfRows
INPUT “Enter Number of Column” NumberOfColumns
INPUT “Enter your symbol ” Symbol
FOR RowCount ← 1 TO NumberOfRows
FOR ColumnCount ← 1 TO NumberOfColumns
OUTPUT Symbol // without moving to next line
NEXT ColumnCount
OUTPUT Newline // move to the next line
NEXT RowCount

Practice Question with Marking Key


Q#1 WRITE A PSEUDOCODE TO FIND THE SUM OF SERIES S=1+2+3+…..
+N
DECLARE Num , Count : Integer
Print “ Enter number till you want to add series of number”
Input Num
For count←1 To Num
Sum← Sum + count
Next Count
Print “Sum of Series of Number till”, Num , “ is =” , Sum

You might also like