Pseudocode Notes
Pseudocode Notes
Pseudocode Notes
Pseudocode
There are 3 programming/Pseudocode constructs:
1. Sequence: it refers that instructions should be executed one after another.
2. Selection: This construct is used to make a decision in choosing an option from many
available options on the basis of a condition. So, if a condition is true then one option
would be chosen while if a condition is false then another option will be chosen.
3. Repetition: this construct is used to repeat a block of code as per the given condition.
Algorithm is step by step solution to a given problem. It is written in plain English statements.
Algorithm is usually transformed into pseudocode or program flowchart.
Once the algorithm is tested in pseudocode or program flowchart then finally it is written in a
specific programming language.
Pseudocode is a false code which consists of plain English statements, mathematical notations
and keywords that are commonly found in high level languages.
It does not follow strict rules and style of any particular programming language. Pseudocode is
used for learning programming concept and to describe ideas before coding begins.
Arithmetic Operators:
In pseudocode arithmetic operators are used to perform arithmetic operations. These
operations are listed below.
Operators Comparison
> Greater than
< Less than
= Equal
>= Greater than or equal
<= Less than or equal
<> Not equal
() Group
Assignment Operator:
Assignment operator is used to assign the value or expression to a variable. The value or
expression is on the right side of assignment operator while the variable is on the left side of
the assignment operator.
Note:
In CAIE exams, mostly ← is used for assignment operator:
Example of pseudocode assignments:
Input in Pseudocode:
OUTPUT:
In pseudocode we indicate the operation of displaying a value or an output or any message by
using either of the following keywords:
OUTPUT
WRITE
PRINT
Note:
In CAIE exam, mostly INPUT, OUTPUT and READ, WRITE keywords are used.
Variable:
It is a named memory space which is used to store values.
Variable usually stores two kind of information.
1. All the input values from user must be stored in a variable.
2. Result of some mathematical operation must be stored in a variable.
These are the rules while assigning name to a variable:
There is no space in a variable name
Variable name should not be a keyword of pseudocode.
Variable name should be relevant.
Example 1:
Write pseudocode that will take two numbers as input, calculates their sum and displays
output.
Solution:
PRINT “Please enter tow numbers to add”
INPUT num1
INPUT num2
Example 2:
Write pseudocode that will take two numbers as input, calculates their product and displays
output.
Example 3:
Write down pseudocode that will take marks of physics, chemistry and math as input,
calculates the average and displays output.
Solution:
PRINT “Please enter marks of physics”
INPUT Phy_marks
PRINT “Please enter marks of chemistry”
INPUT Chem_marks
PRINT “Please enter marks of maths”
Repetition:
The process is repeating a block of pseudocode is called as repetition. It is also known as
Looping or Iteration.
There are three types of repetition statements
1. FOR…TO…NEXT
2. WHILE…DO…ENDWHILE
3. REPEAT…UNTIL
There is a variable in FOR…TO…NEXT loop for controlling the number of iterations and is
known as a control variable. The name of the control variable is usually “Count”.
We specify the initial (lower) and final (higher) values of the control variable in the
opening statement of the loop. These initial and final values are not restricted to the
numerical values only, they can be variables as well.
The control variable is automatically incremented by ‘1’ each time the loop ends.
The value of the control variable is tested at the beginning of the loop and loop is
repeated until the value of control variable is less than or equal to the specified final
value.
Example 4:
Write pseudocode that will take 10 numbers as input and print their average by using FOR … TO
… NEXT loop.
Solution:
Total = 0
FOR count = 1 TO 10
PRINT “Enter Number”
INPUT num
Total = Total + num
NEXT
Avg = Total / 10
PRINT “Average of 10 Numbers is: “; Total
Few things to remember:
Example 6:
Mr. Ali wants to calculate the average marks for his class of 28 students for Urdu, Islamiat and
Pak. Studies. Write down pseudocode that will help him to accomplish this task.
The loop is repeated until a condition is true and halted when the condition is false.
Condition is tested at eh beginning of the loop.
The statements/instruction between DO and ENDWHILE keywords are repeated.
Example 7:
Write pseudocode that will take numbers input and add them while the input number is greater
than or equal to 0. Print the final result.
Solution:
Total = 0
PRINT “Input Number “
INPUT num
Example 8:
Write pseudocode that will take numbers input, add them and calculates the average while the
input number is greater than or equal to 0. Print the average of all input numbers.
Solution:
Total = 0
Note:
Although there is a difference of operation between FOR…TO…NEXT loop and
WHILE…DO…ENDWHILE Loop but still there is a possible way to rewrite a code written in
FOR…TO…NEXT loop into WHILE…DO…ENDWHILE loop.
Example 9:
Rewrite the pseudocode given below by using WHILE…DO...ENDWHILE loop.
Total = 0
FOR count = 1 TO 10
PRINT “Enter Number”
INPUT num
Total = Total + num
NEXT
Solution:
Total = 0
Count = 1
WHILE Count <= 10
DO
PRINT “Enter Number”
INPUT num
Total = Total + num
Count = Count + 1
ENDWHILE
Avg = Total / 10
PRINT “Average of 10 Numbers is : “, Total
Example 10:
Rewrite the pseudocode given below by using FOR…TO…NEXT loop.
Total = 0
Count = 1
WHILE Count <= 15
DO
PRINT “Enter a number”
INPUT num
Total = Total + num
Count = Count + 1
ENDWHILE
Solution:
Total = 0
FOR Count = 1 TO 15
PRINT “Enter a Number “
INPUT num
Total = Total + num
NEXT
Example 11:
A pseudocode is given below whih will take age of 18 students and print out the minimum age
of a student. Rewrite all the pseudocode using WHILW…DO…ENDWHILE loop.
Example 12:
Rewrite the code written below using WHILE…DO…ENDWHILE Loop.
Total_temp = 0
Total_Shrs = 0
FOR Count = 1 To 365
PRINT “Please enter the temperature”
INPUT temp
Total_temp = Total_temp + temp
PRINT “Please enter sunshine hours”
INPUT Shours
Total_Shrs = Total_Shrs + Shours
NEXT
Avgtemp = Total_temp / 365
AvgShrs = Total_Shrs / 365
Solution:
REPEAT…. UNTIL:
It is a repetition statement that is used when we don’t know how many times an instruction or
set of instructions is to be repeated.
Example 13:
Write the pseudocode that will take numbers as input, add and gives the total as output. The
loop will continue until “0” is given as input.
Solution:
Total = 0
REPEAT
PRINT “Enter a number to add”
INPUT num
Total = Total + num
UNTIL num is “0”
PRINT Total
Example 14:
Selection Statements:
These are also known as conditional statements.
There are two types of selection statements.
Example 15:
Write pseudocode that will take a number as input and tells whether a number is positive,
negative or zero.
Solution:
PRINT “Enter a number”
INPUT num
Exmple 16:
Write pseudocode that will take marks in an exam as input and will tell the grade A* for 90 to
100, A for 80 to 89, B for 70 to 79, C for 60 to 69 and F grade for 0 to 59.
PRINT “Enter Marks”
INPUT marks
IF maks >= 90 AND marks <= 100
THEN
PRINT “The Grade is A* “
ELSE IF marks >= 80 AND marks < 90
THEN
PRINT “ The Grade is A “
ELSE IF marks >= 70 AND marks < 80
THEN
PRINT “ The Grade is B “
ELSE IF marks >= 60 AND marks < 70
THEN
Note: Each IF statement and ELSE IF statement must have an ENDIF statement to terminate.
Example 17:
Write pseudocode that performs the following: Ask a user to enter a number. If the number is
between 0 and 10, write the word blue. If the number is between 10 and 20, write the word
red. If the number is between 20 and 30, write the word green. If it is any other number, write
that it is not a correct color option.
Example 18:
Write down pseudocode that will take three numbers as input and tells
a. The Largest Number
b. The Smallest Number
Solution: (a)
PRINT “Enter three numbers “
INPUT num1
INPUT num2
INPUT num3
In Short, CASE … OF… OTHERWISE … ENDCASE is used in place of IF …. THEN … ELSE … ENDIF
statement when we have to make a selection of route from too many available routes.
Example 19:
Write down pseudocode that will take a number as input (from 1 to 7) and print the day name
for corresponding number e.g 1 for Monday 2 for Tuesday and so on.
Solution:
PRINT “Enter a number to display the day name. “
INPUT daynum
CASE daynum OF
1: dayname = “Monday”
2: dayname = “Tuesday”
3: dayname = “Wednesday”
4: dayname = “Thursday”
5: dayname = “Friday”
6: dayname = “Saturday”
7: dayname = “Sunday”
OTHERWISE
PRINT “Error”
daynum = “unknown”
ENDCASE
PRINT “The day is : “, dayname
Example 20:
Write a pseudocode that will take two numbers as input perform any of the basic operation (+,
-, *, /) as per user requirements.
Solution:
PRINT “Enter two numbers”
INPUT num1
INPUT num2
PRINT “Enter the operation”
INPUT Operator
CASE Operator OF
+ : Ans = num1 + num2
- : Ans = num1 - num2
/ : Ans = num1 / num2
*: Ans = num1 * num2
OTHERWISE
PRINT “Invalid Operator”
Ans = “Invalid”
ENDCASE
PRINT “Answer is: “, Ans
Question 1:
a. Write an algorithm, using pseudocode or a flowchart, which
a. Inputs a set of positive numbers (which end with -1)
b. Outputs the average (mean) value of the input numbers
c. Outputs the value of the largest (highest) number input.
b. Writhe an algorithm, using pseudocode or a flowchart, which
a. Inputs a whole number (which is > 0)
b. Calculates the number of digits in the number
c. Outputs the number of digits and the original number
(E.g. 147 would give an output of 3, 147)
Solution: (a)
Highest = -100; total = 0; count = 0 (1 mark) initialize values highest can’t be 0
INPUT number (1 mark) inputs in correct place
WHILE number <> -1 DO (1 mark) loop until -1 is input
Total = total + number (1 mark) calculate number total
Count = count + 1 and count numbers input
If number > highest then highest then highest = number (1 mark) highest
INPUT number
ENDWHILE
Average = total / count (1 mark) calculate average values
PRINT average, highest and output average and
highest value
Solution: (b)
D=0 (1 mark) initialize value
INPUT number (1 mark) input number and set variable to
this
T = number Number
REPEAT (1 mark) correct loop
T = T / 10 (1 mark) method to find number of digits
D=D+1 (1 mark) counting number of digits
UNTIL T < 1
PRINT number, D (1 mark) correct output outside the loop
(Note: there are other ways of finding number of digits e.g.
IF number > 0 THEN d = 1
ELSE IF number > 9 THEN d = 2
…………………………………………………………………………..
ELSE IF number > 999999 THEN d = 7 etc)
[4]
Question 2:
Write a program that should get 50 values from user,
Output how many values are greater than 100.
Output total of those values that are less than 50.
Solution:
FOR I = 1 TO 50
INPUT num
IF num > 100
THEN C = C + 1
Question 3:
Write a pseudocode that prints the sale of a shopkeeper for a week.
Solution:
FOR I = 1 TO 7
INPUT Customer
FOR b = 1 TO Customer
INPUT sale
Total = total + sale
NEXT b
PRINT “Total sale for day “, I , “=”, total
NEXT i
Question 4:
Write a pseudocode that print table of 7 till 100, using REPEAT UNTIL LOOP.
I=0
REPEAT
PRINT i
I = I + 7
UNTIL (I < 100)
Question 5:
Write a pseudocode that print table of 7, using FOR TO NEXT LOOP.
FOR I = st TO ev
Ans = t * st
PRINT t * st “ = “ ans
NEXT i
1 Total = 100.00
2 PRINT 'Enter the height of each member of your class, one at a
time, when prompted'
3 FOR Count = 1 TO 30
4 PRINT 'Enter a height in metres'
5 INPUT Height
6 Total = Total + Height
7 PRINT Total / 30
8 Count = Count + 1
9 NEXT Count
Error 1 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Error 2 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Error 3 .......................................................................................................................................
...................................................................................................................................................
.................................................................................................................................................[3]
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................... [1]
Q. 5 a 0478 w 18 QP 23
(a) Complete the trace table for the input data: 7 [4]
(b) Complete the trace table for the input data: 2 [2]
(b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose each set.
Test data set 1 ..........................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
Reason .....................................................................................................................................
..............................................................................................................................................[4]
0478 w 18 QP 21
0478 S 18 QP 23
Q. 3 This section of program code reads the contents of the array, totals the numbers and prints
out the sum and average of the numbers. Assume the array is full. Complete the four missing
items by writing them in the spaces provided in this code.
1 Numbers[1:30]
2 Total = 0
3 .............................................. = 0
INPUT Number
IF Number > 100
THEN OUTPUT ″The number is too large″
ELSE OUTPUT ″The number is acceptable″
ENDIF
(a) Describe the purpose of the algorithm. [2]
(b) (i) The algorithm only allows one attempt at inputting an acceptable value.
State how you would change the algorithm so that it continues until a suitable input is supplied.
[1]
(ii) Re-write the algorithm in full, using pseudocode, to implement your answer to part (b)(i).
[3]
0478 S 18 QP 22
Q. 3 This pseudocode algorithm inputs two non-zero numbers and a sign, and then performs the
calculation shown by the sign. An input of zero for the first number terminates the process.
(a) Complete the trace table for the input data: [3]
5, 7, +, 6, 2, –, 4, 3, *, 7, 8, ?, 0, 0, /
Number 1 Number 2 Sign Answer OUTPUT
(b) Show how you could improve the algorithm written in pseudocode by writing an alternative
type of conditional statement in pseudocode. [3]
0478 S 18 QP 21
Q. 2 (a)
Write an algorithm to input 1000 numbers. Count how many numbers are positive and how many
numbers are zero. Then output the results. Use either pseudocode or a flowchart.
[6]
(b) Give one change you could make to your algorithm to ensure initial testing is more
manageable. [1]
Q. 3 The global trade item number (GTIN-8) barcode has seven digits and a check digit.
This pseudocode algorithm inputs seven digits and calculates the eighth digit, then outputs the
GTIN-8.
DIV(X,Y), finds the number of divides in division for example DIV(23,10) is 2.
MOD(X,Y), finds the remainder in division for example MOD(23,10) is 3.
FOR Count 1 TO 7
INPUT Number
Digit(Count) Number
NEXT
Sum (Digit(1)+Digit(3)+Digit(5)+Digit(7))*3+Digit(2)+Digit(4)+Digit(6)
IF MOD(Sum,10) <> 0
THEN Digit(8) DIV(Sum,10)*10 + 10 - Sum
ELSE Digit(8) 0
ENDIF
OUTPUT "GTIN-8"
FOR Count 1 TO 8
OUTPUT Digit(Count)
NEXT
Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT
Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT
[5]
Q. 3 (b) Explain how you would change the algorithm to input eight digits (seven digits and the check
digit) and output if the check digit entered is correct or not. [3]
0478 W 17 QP 23
Section B
Q. 2 This section of program code asks for 80 numbers between 100 and 1000 to be entered. It
checks that the numbers are in the correct range, and stores them in an array. It counts how
many of the numbers are larger than 500 and then outputs the result when the program is
finished.
1 Count = 0
2 FOR Index = 1 TO 80
3 INPUT 'Enter a number between 100 and 1000', Number
4 WHILE Number = 99 AND Number = 1001
5 INPUT 'This is incorrect, please try again', Number
6 ENDWHILE
7 Num[80] = Number
8 IF Number > 500 THEN Count = Count + 1
9 UNTIL Index = 80
10 PRINT Index
11 PRINT ' numbers were larger than 500'
There are four lines of code that contain errors.
State the line number for each error and write the correct code for that line.
Error 1 Line Number .............................
Correct Code .................................................................................................................................
Error 2 Line Number .............................
Correct Code .................................................................................................................................
Error 3 Line Number .............................
Correct Code .................................................................................................................................
Error 4 Line Number .............................
Correct Code ............................................................................................................................ [4]
0478 W 17 QP 22
• use this value to set up how many other numbers are to be input
• calculate and output the total and the average of these numbers. [6]
0478 S 17 QP 23
Q. 2 This section of program code asks for 50 numbers to be entered. The total and average of
the numbers are calculated.
1 Total = 0
2 Counter = 50
3 PRINT 'When prompted, enter 50 numbers, one at a time'
4 REPEAT
5 PRINT 'Enter a number'
6 INPUT Number
7 Total + Number = Total
8 Number = Number + 1
9 UNTIL Counter = 50
10 Average = Number * Counter
11 PRINT 'The average of the numbers you entered is ', Average
FOR I = 1 TO 300
INPUT Name[I]
NEXT I
............................................................................................................................................... [2]
(b) Identify, using pseudocode, another loop structure that the algorithm in part (a) could have
used.
............................................................................................................................................... [1]
(c) Write an algorithm, using pseudocode, to input a number between 0 and 100 inclusive. The
algorithm should prompt for the input and output an error message if the number is outside this
range.
.................................................................................................................................................[3]
0478 S 17 QP 22
Section B
(b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose
each set.
Test data set 1 ...........................................................................................................................
Reason ......................................................................................................................................
Test data set 2 ...........................................................................................................................
Reason ...................................................................................................................................[4]
Q .4 An algorithm has been written in pseudocode to input 100 numbers and print out the sum.
A REPEAT … UNTIL loop has been used.
Count = 0
Sum = 0
REPEAT
INPUT Number
Sum = Sum + Number
Count = Count + 1
UNTIL Count > 100
PRINT Sum
(b) Rewrite the correct algorithm using a more suitable loop structure.
............................................................................................................................................... [3]