Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

IT Problem Solving & Programming

Control Constructs:
Sequence
Selection
Repetition/Loops/Iteration

The Repetition Construct (Iteration or Loops)


These are used to repeat statements or instructions.

Types of Loops:
1. For Loop
2. While Loop
3. Repeat Until Loop

The FOR LOOP

This is a definite loop. The loop is repeated for a set number of times.
All the statements between the FOR and the ENDFOR are the loop block or compound statement. A FOR loop can be
used as a counter to count the number of times calculations are carried out. Once a FOR loop is being used, a counter
variable must be declared. The counter variable counts the number of times the loop is executed and thus should always
be declared as an integer. A counter variable can be a letter, the word count or counter, or your initial etc (A, count,
counter, FD etc.)

PSEUDOCODE FORMAT:

FOR countervariable🡨 startvalue TO stopvalue DO


Compound statement/Actions to be repeated
ENDFOR

PASCAL FORMAT:
FOR countervariable := startvalue TO stopvalue DO
BEGIN
Compound statement/Actions to be repeated;
END;

FLOWCHART FORMAT:

False FOR countervariable


startvalue TO stopvalue
Do
True

Compound Statement(s)
(symbols will vary)
ACTIVITY:
Write a psuedocode to output the message “Are we there yet” 100 times.

Pseudocode Solution:
{This algorithm will output the message “Are we there Flowchart Solution
yet” 100 times}

Algorithm Message Start

Var C : integer
Var C : Integer

Start
C 0
C🡨0

FOR C 🡨 1 to 100 DO False


FOR C 1 to
Print “Are we there yet”
100
ENDFOR
True
Stop
Print “Are we there
yet”

Stop
Pascal Solution:

{This program will output the message “Are we there


yet” 100 times}

Program Message;

Uses crt;

Var C : integer;

Begin

C := 0;

FOR C := 1 to 100 DO
Begin
Writeln (‘Are we there yet’);
End;

End .
WHILE LOOP

This loop is used when you want to repeat instruction for an indefinite number of times. The While loop is a pre - tested
loop. This means that the condition is tested before the loop’s body is executed. An initial value is assigned to a variable
and the instructions are repeated until that variable reaches a certain value or point. If the test before the loop is false
then the body will not be executed. The loop is ended when the condition becomes false. The value that is used to end
the loop is call the sentinel value.

PSUEDOCODE FORMAT:
While condition Do
Compound statement/Actions to be repeated
Endwhile

PASCAL FORMAT:
While (condition) Do
BEGIN
Compound statement/Actions to be repeated;
End;

FLOWCHART FORMAT:

False While Condition Do

True

Compound Statement(s)
(symbols will vary depending on the statement(s)

REPEAT UNTIL LOOP

This loop is also used when you want to loop for an indefinite number of times. The Repeat until loop is a post - tested
loop. The condition is tested after the loop’s body is executed. An initial value is assigned to a variable and the
instructions are repeated until that variable reaches a certain value or point. If the test after the loop is true then the
body will not be executed. The loop is ended when the condition becomes true.

ACTIVITY: Write an algorithm and flowchart to read a sequence of numbers terminated by 0 and print the sum of all
numbers.

PSEUDOCODE FORMAT:

Repeat
Compound statement/Actions to be repeated
Until Condition

PASCAL FORMAT:

Repeat
Compound statement/Actions to be repeated;
Until (Condition);
FLOWCHART FORMAT:

Compound Statements/actions to be repeated

Until Condition False

True

ACTIVITY:

Write an algorithm, pascal code and flowchart to read a sequence of numbers terminated by 0 and print the sum of all
numbers.
NB: Both While and Repeat Until can be used since the number of times the statements to be repeated is unknown.
While Loop Solution
{This program reads a sequence of numbers terminated
Pseudocode by 0. It will also print the sum of all the numbers}

{This algorithm reads a sequence of numbers Program SequenceOfNumbers;


terminated by 0. It will also print the sum of all the
numbers} Uses crt;

Algorithm SequenceOfNumbers Var num: integer;


sum:integer;
Var num: integer
sum:integer Begin

Start num := 0;
sum := 0;
num 🡨 0
sum 🡨 0 Writeln (‘Enter a number. If you want to terminate
enter 0’);
Print ‘Enter a number. If you want to terminate enter 0” Read (num);
Read num
WHILE (num < > 0) DO
WHILE num < > 0 DO Begin
sum 🡨 sum + num sum := sum + num;
Print ‘Enter a number. If you want to terminate Writeln (‘Enter a number. If you want to
enter 0” terminate enter 0’);
Read num Read (num);
End;
ENDWHILE
Writeln (‘The sum of the numbers is’, sum);
Print ‘The sum of the numbers is’, sum

Stop
End .
While loop (continued)

Flowchart Solution
Pascal
Start
True

Var num: integer sum sum + num


sum:integer

Print ‘Enter a number. If you


want to terminate enter 0”
num 0
sum 0

Read num

Print ‘Enter a number. If you


want to terminate enter 0”

Read num Print ‘The sum of the numbers is’,


sum

False Stop
WHILE num <
>0
Repeat Until Loop Solution

Pseudocode Solution Pascal Solution

{This algorithm reads a sequence of numbers {This program reads a sequence of numbers terminated
terminated by 0. It will also print the sum of all the by 0. It will also print the sum of all the numbers}
numbers}
Program SequenceOfNumbers;
Algorithm SequenceOfNumbers
Uses crt;
Var num: integer
sum:integer Var num: integer;
sum:integer;
Start
Begin
num 🡨 0
sum 🡨 0 num := 0;
sum := 0;
Repeat
Repeat
Print ‘Enter a number. If you want to terminate Writeln (‘Enter a number. If you want to
enter 0’ terminate enter 0’);
Read num Read (num);

sum 🡨 sum + num sum := sum + num;

UNTIL num = 0 UNTIL (num = 0);

Print ‘The sum of the numbers is’, sum Writeln (‘The sum of the numbers is’, sum);

Stop
End .

Start
Var num: integer
sum:integer

num 0
sum 0

Print ‘Enter a number. If you


want to terminate enter 0”

Read num

sum sum + num

True Until num < > 0

False

Print ‘The sum of the numbers is’,


sum

Stop

You might also like