You are on page 1of 2

Indefinite Loops

Loops that iterate while or until a specific condition is met. It does not run for a set number of times.

1. WHILE - DO Loop (The condition is checked at the start, invalid data cannot execute in the loop)
2. REPEAT - UNTIL Loop (The condition is checked at the end, all data gets atleast a single chance to execute
in the loop)

Example : User is expected to enter a value less than or equal to 5.

WHILE DO LOOP
SET X TO 1

WHILE X <= 5 DO
SEND “Enter a number less than or equal to 5 to continue” TO DISPLAY
RECEIVE X FROM (INTEGER)KEYBOARD
ENDWHILE

REPEAT UNTIL LOOP (Python doesn’t allow to construct this type of loop)
SET X TO 1

REPEAT
SEND “Enter a number less than or equal to 5 to continue” TO DISPLAY
RECEIVE X FROM (INTEGER)KEYBOARD
UNTIL X > 5

( The above loops stop when the value input for X is more than 5)

Q)Write a pseudocode to enter some words and the program stops when “end” is entered.
(NOT EQUAL in pseudo code is < >)

SET Word TO “ “

WHILE Word < > “end” DO


SEND “Enter a word, “end” stops the process” TO DISPLAY
RECEIVE Word FROM (STRING)KEYBOARD
ENDWHILE

Random Numbers

It is a function which allows to handle some random events, such as to guess a number from a set of
numbers which are generated automatically, flip a coin, roll a dice, etc..

Most high level programming languages have an inbuilt function to perform this, which is called the
Random Number Generator.

Python Code

import random - calling a special library / module

Num = random.randint(1,10) - generates a number between 1 and 10 inclusive.


How to Make Programs Easy to Read

1. Use comments to explain the lines of codes to understand what it does

2. Use meaningful / descriptive names for identifiers(variables, constants, arrays, sub programs) which
helps to know the purpose of the usage of identifiers used in the program.

3. Use of indentation to make it easier to spot where the loops and conditions begin and end.

4. Leave some white spacez or blank lines to separate the different blocks or parts of the code to recognise
easily.

Example:
// Initialise Num
SET TO 0

// The loop runs while the value of Num is not equal to -1


WHILE < > TO -1 DO
SEND “Enter a whole number : “ TO DISPLAY
RECEIVE FROM (INTEGER)KEYBOARD
ENDWHILE

You might also like