Information Technology
Problem Solving and Program Design
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.
Iteration is a repetition of a statement or statements in a program.
The three common loop structures are:
1. FOR
2. While-Do
3. Repeat Until
There are two types of loops:
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
The basic structure of a loop is:
Initialization
Loop Header
Loop statements/ Loop body /Loop block
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
Pascal Program Pseudocode
For ... to ... do
Syntax :
The control variable (e.g. COUNTER)
For variable := fromwhat to what do FOR variable_name<start value> TO <end
begin value> DO
:
: Statement 1
statements / commands
: Statemnet 2
:
end; …
END FOR
While...do
The syntax is :
While conditions do WHILE <condition statement> DO
begin
: Statement 1
(statements / commands)
: Statement 2
end;
…
END WHILE
Repeat ... Until.
The syntax is : REPEAT
Repeat Statement 1
:
statements / commands Statemnet 2
:
Until condition; …
UNTIL <condition statement>
Information Technology
Problem Solving and Program Design
Examples
For Loop
This program will read 10 numbers and find the sum of those numbers.
START
DECLARE counter, Sum, num AS INTEGER
sum num 0
FOR counter 1 to 10 DO
PRINT “Enter the number”
Read num
SumSum + num
END FOR
Print “The sum is”, Sum
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
DECLARE counter, Sum, num AS INTEGER
sum num counter 0
WHILE counter <= 10 DO
PRINT “Enter the number”
Read num
SumSum + num
counter counter + 1
END WHILE
Print “The sum is”, Sum
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
UNTIL Number >10
Print “The sum is”, Sum
STOP