Professional Documents
Culture Documents
Looping means repeating a statement over and over until some condition is met. Loops can also
be referred to as constructs used for repeating parts of a program. That is, they will repeatedly
execute a section of a program until a condition is satisfied.
Sequential control is indicated by writing one action after another, each action on a line by
itself, and all actions aligned with the same indent. The actions are performed in the sequence
(top to bottom) that they are written.
Initialize to set (variables) to their starting values at the beginning of a program or subprogram.
2. While-Do
3. Repeat Until
An infinite loop (also known as an endless loop or unproductive loop or a continuous loop) is a
loop which never ends. Inside loop, statements are repeated forever. You do not know how many
times you will need to repeat the statements in a loop structure. The while-do and Repeat until
loop structures can be used in this type of scenario.
For definite loops the number of iterations is known before we start the execution of the body of
the loop. All three loop structures can be used in this type of scenario
Initialization
Loop Header
Conclusion
Initialization- Loops use a variable to control the amount of iterations. We need to initialize the
variable to a start value or read an initial value into the variable.
Loop Header-This section is the entry point to the loop. It states the type of loop as well as a
logical test against the controlling variable. It may have the initialization if it is a FOR loop.
Loop Block-We must specify what statements are to be repeated by the computer. The
repetitive statements are normally placed in the loop block.
Conclusion-This is a simple keyword(s) to signify the end of the loop structure.
Information Technology
Problem Solving and Program Design
END FOR
While...do
The syntax is :
END WHILE
Repeat Statement 1
:
statements / commands Statemnet 2
:
Until condition; …
For Loop
This program will read 10 numbers and find the sum of those numbers.
START
sum num 0
FOR counter 1 to 10 DO
Read num
SumSum + num
END FOR
STOP
While Loop
Eg.1This program will read some numbers and find the sum of those numbers.
START
DECLARE counter, Sum, num AS INTEGER
sum num 0
PRINT “ Enter 9999 to end the program”
READ num
WHILE num<> 9999 DO
SumSum + num
PRINT “Enter the number”
Read num
END WHILE
Print “The sum is”, Sum
STOP
Information Technology
Problem Solving and Program Design
Eg.2 This program will read 10 numbers and find the sum of those numbers.
START
Read num
SumSum + num
counter counter + 1
END WHILE
STOP
REPEAT…UNTIL LOOP
This program will read some numbers and find the sum of those numbers.
START
DECLARE counter, Sum, number AS INTEGER
sum number 0
REPEAT
WRITE "Positive number?"
READ Number
SumSum + number