Sequence and Selection Control Structure
Sequence and Selection Control Structure
Lines are indented by four spaces to indicate that they are contained within a statement in a
previous line. Where it is not possible to fit a statement on one line any continuation lines are
indented by two spaces from the margin. In cases where line numbering is used, this
indentation may be omitted. Every effort will be made to make sure that code statements are
not longer than a line of code, unless this is necessary. Note that the THEN and ELSE clauses
of an IF statement are indented by only two spaces. Cases in CASE statements are also
indented by only two spaces.
Case
Keywords are in upper case, e.g. IF, REPEAT, PROCEDURE. Identifiers are in mixed case
with upper case letters indicating the beginning of new words, e.g. NumberOfPlayers.
Meta-variables – symbols in the pseudocode that should be substituted by other symbols are
enclosed in angled brackets < >.
Literals
Literals (values) of the above data types are written as follows:
• Integer written as normal in the denary system, e.g. 5, –3
• Real always written with at least one digit on either side of the decimal point, zeros being
added if necessary, e.g. 4.7, 0.3, –4.0, 0.0
• Char a single character delimited by single quotes, e.g. ꞌxꞌ, ꞌcꞌ, ꞌ@ꞌ
• String delimited by double quotes. A string may contain no characters (i.e. the empty
string), e.g. "This is a string", ""
• Boolean TRUE, FALSE
Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mixed
case using Camel case, e.g. FirstName. They can only contain letters (A–Z, a–z) and digits
(0–9). They must start with a capital letter and not a digit. Accented letters and other
characters, including the underscore, should not be used. Keywords cannot be used as
identifiers.
Data Structures:
Variables:
Named data store whose value can change during the execution of the program.
Variable declarations
Declarations are made as follows:
DECLARE <identifier> : <data type>
Example – variable declarations
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN
Constant:
Named data store whose value cannot change during the execution of the program.
It is good practice to use constants if this makes the pseudocode more readable, and easier to
update if the value of the constant changes. Constants are declared by stating the identifier
and the literal value in the following format:
CONSTANT <identifier> ← <value>
Example – CONSTANT declarations
CONSTANT HourlyRate ← 6.50
CONSTANT DefaultText ← "N/A"
Only literals can be used as the value of a constant. A variable, another constant or an
expression must never be used.
Arrays
Arrays are fixed-length structures of elements of identical data type, accessible by
consecutive index numbers. It is good practice to explicitly state what the lower bound of the
array (i.e. the index of the first element) is because this defaults to either 0 or 1 in different
systems. Generally, a lower bound of 1 will be used. Square brackets are used to indicate the
array indices.
Declaring arrays
1D and 2D arrays are declared as follows (where l, l1, l2 are lower bounds and u, u1, u2 are
upper bounds):
DECLARE <identifier> : ARRAY[<l>:<u>] OF <data type>
DECLARE <identifier> : ARRAY[<l1>:<u1>,<l2>:<u2>] OF <data type>
Example – array declaration
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
Using arrays in the main pseudocode statements, only one index value is used for each
dimension in the square brackets.
Example – using arrays
StudentNames[1] ← "Ali"
NoughtsAndCrosses[2,3] ← ꞌXꞌ
StudentNames[n+1] ← StudentNames[n]
Arithmetic Operator:
+ addition
– subtraction
* multiplication
/ division
^ raised to the power of
The integer division operators MOD and DIV can also be used.
DIV(<identifier1>,<identifier2> ) Returns the quotient of identifier1 divided by identifier2
with the fractional part discarded. MOD(<identifier1>,<identifier2>) Returns the remainder
of identifier1 divided by identifier2 The identifiers are of data type integer.
Examples – MOD and DIV
DIV(10, 3) returns 3
MOD(10, 3) returns 1
Multiplication and division have higher precedence over addition and subtraction (this is the
normal mathematical convention). However, it is good practice to make the order of
operations in complex expressions explicit by using parentheses
Assignments
The assignment operator is ←. Assignments should be made in the following format:
<identifier> ← <value>
The identifier must refer to a variable (this can be an individual element in a data structure
such as an array). The value may be any expression that evaluates to a value of the same data
type as the variable. Example – assignments
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
Precedence of Operators
()
^
*, /, MOD,DIV
+, -
Relational operators
NOT
AND
OR
OUTPUT: Sum = 15
1. Write pseudocode to find sum of two numbers entered by user.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
Sum Num1 + Num2
OUTPUT “Sum = “, Sum
Write pseudocode to find area and perimeter of a rectangle. Length and width will be entered
by user.
OUTPUT “Enter length”
INPUT Length
OUTPUT “Enter Width”
INPUT Width
Area Length * Width
Perimeter 2 * (Length + Width)
OUTPUT “Area of rectangle = “, Area
OUTPUT “Perimeter of rectangle = “, Perimeter
3. Write pseudocode to find area of a circle. Radius of circle will be entered by user.
CONSTANT Pi 3.142
OUTPUT “Enter radius”
INPUT Radius
Area Pi * Radius ^ 2
OUTPUT “Area of circle = “, Area
4. Write pseudocode to find square and square root of a number entered by user.
OUTPUT “Enter a number”
INPUT Num
Square Num ^ 2
SquareRoot Num ^ (½)
OUTPUT “Square = “, Square
OUTPUT “Square root = “, SquareRoot
5. Write pseudocode which inputs two numbers and outputs the larger number.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
IF Num1 > Num2
THEN
OUTPUT Num1, “ is larger.”
ELSE
OUTPUT Num2, “ is larger.”
END IF
6. Write pseudocode which inputs a number and output whether the number is positive or
not.
OUTPUT “Enter a number”
INPUT Num
IF Num > 0
THEN
OUTPUT Num, “is positive”
ELSE
OUTPUT Num, “is not positive”
END IF
7. Write pseudocode which inputs a number and output whether the number is even or odd.
OUTPUT “Enter a number”
INPUT Num
IF MOD (Num,2) = 0
THEN
OUTPUT Num, “is even”
ELSE
OUTPUT Num, “is odd”
END IF
8. Write Pseudocode which inputs three numbers from the user and outputs the largest
number.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
OUTPUT “Enter third number”
INPUT Num3
IF Num1 > Num2 AND Num1 > Num3
THEN
OUTPUT Num1, “ is the largest number.”
ELSE
IF Num2 > Num3
THEN
OUTPUT Num2, “ is the largest number.”
ELSE
OUTPUT Num3, “ is the largest number.”
END IF
END IF
CASE OF <identifier>
<value1>: <Statement>
<value2>: <Statement>
<value3>: <Statement>
OTHERWISE: Statement
END CASE
It is best practice to keep the branches to single statements as this makes the pseudocode
more readable. Similarly, single values should be used for each case. If the cases are more
complex, the use of an IF statement, rather than a CASE statement, should be considered.
Each case clause is indented by two spaces. They can be considered as continuations of the
CASE statement rather than new statements. Note that the case clauses are tested in sequence.
When a case that applies is found, its statement is executed, and the CASE statement is
complete. Control is passed to the statement after the ENDCASE. Any remaining cases are
not tested.
If present, an OTHERWISE clause must be the last case. Its statement will be executed if
none of the preceding cases apply
9. Write pseudocode which inputs day number(1-7) and outputs corresponding day name e.g
1 is Monday.
OUTPUT “Enter day number (1-7)”
INPUT DayNum
CASE OF DayNum
1: OUTPUT “Monday”
2: OUTPUT “Tuesday”
3: OUTPUT “Wednesday”
4: OUTPUT “Thursday”
5: OUTPUT “Friday”
6: OUTPUT “Saturday”
7: OUTPUT “Sunday”
OTHERWISE OUTPUT “Invalid day number”
END CASE
10. Write pseudocode which inputs two numbers and an operator (+, -, *, /) and output the
result according to the operator selected by user using CASE OF statement.
OUTPUT “Enter first number”
INPUT Num1
OUTPUT “Enter second number”
INPUT Num2
OUTPUT “Enter operator (+,-,*,/) ”
INPUT Op
CASE OF Op
“+”: OUTPUT “Sum = “, Num1 + Num2
“-”: OUTPUT “Difference = “, Num1 – Num2
“*”: OUTPUT “Product = “, Num1 * Num2
“/”: OUTPUT “Division Answer = “, Num1 / Num2
OTHERWISE: OUTPUT “Invalid operator”
END CASE