8. Programming
8. Programming
YOUR NOTES
IGCSE Computer Science CIE
CONTENTS
Data Types
Variables & Constants
Input & Output
Sequence
Selection
Iteration
Totalling & Counting
String Handling
Arithmetic, Logical & Boolean Operators
Nested Statements
Procedures & Functions
Local & Global Variables
Library Routines
Maintaining Programs
Page 1 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
It is important to choose the correct data type for a given situation to ensure accuracy and
efficiency in the program.
Page 2 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
YOUR NOTES
Worked Example
Name and describe the most appropriate programming data type for each of the
examples of data given. Each data type must be different
[6]
Data: 83
Data type name: Integer [1]
Data type description: The number is a whole number [1]
Data: [email protected]
Data type name: String [1]
Data type description: It is a group of characters [1]
Data: True
Data type name: Boolean [1]
Data type description: The value is True (or could be False) [1]
Page 3 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Examples in Pseudocode:
Declare a variable called 'score' with a value of 10
score ← 10
Examples in Python:
Declare a variable called 'score' with a value of 10
score = 10
Examples in Java:
Declare a variable called 'score' with a value of 10
int score = 10;
Page 4 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Exam Tip
Exam questions will ask you to write pseudocode statements, rather than in a
specific language
Worked Example
The variables 'name' and 'age' are used to store data in a program:
name stores the user’s name
age stores the user’s age
Write pseudocode statements to declare the variables name and age.
[2]
DECLARE name : STRING [1]
DECLARE age : INTEGER [1]
Page 5 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Pseudocode example:
OUTPUT "Hello, ", name
Python example:
print("Hello, ", name)
Java example:
System.out.println("Hello, " + name);
Page 6 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Pseudocode example:
OUTPUT "Enter your name"
INPUT name
Python example:
name = input("Enter your name: ")
Java example:
Scanner input = new Scanner(System.in);
name = Console.Readline()
Exam Tip
Remember to prompt the user for input clearly, and format the output to make it
readable and understandable.
Page 7 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Pseudocode example:
PRINT "Hello, World!"
x←5
y←10
z← x + y
PRINT z
Python example:
print("Hello, World!")
x=5
y = 10
z=x+y
print(z)
Java example:
System.out.println("Hello, World!");
int x = 5;
int y = 10;
int z = x + y;
System.out.println(z);
Dim x As Integer = 5
Dim y As Integer = 10
Dim z As Integer = x + y
Page 8 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Page 9 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Page 10 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
IF x > 0
THEN
ENDIF
Python example:
x=5
if x > 0:
print("x is positive")
Java example:
int x = 5;
if (x > 0) {
System.out.println("x is positive");
If x > 0 Then
Console.WriteLine("x is positive")
End If
IF ELSE Statements
Page 11 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
If else statements are used to execute one set of statements if a condition is true and a different YOUR NOTES
set of statements if the condition is false. They have the following syntax:
IF condition
THEN
Instructions
ELSE
Instructions
ENDIF
Pseudocode example:
x←5
IF x > 0
THEN
ELSE
PRINT “x is negative”
ENDIF
Python example:
x=5
if x > 0:
print("x is positive")
else:
print("x is negative")
Java example:
int x = 5;
if (x > 0) {
System.out.println("x is positive");
} else {
System.out.println("x is negative");
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Console.WriteLine("x is positive")
Else
Console.WriteLine("x is negative")
End If
IF ELSE IF Statements
If else if statements are used to test multiple conditions and execute different statements for
each condition. They have the following syntax:
IF condition
THEN
Instructions
ELSE IF condition
THEN
Instructions
ELSE
Instructions
ENDIF
Pseudocode example:
x← 5
IF x > 0
THEN
ELSE IF x < 0
THEN
PRINT “x is negative”
ELSE
PRINT “x is 0”
ENDIF
Python example:
Page 13 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is 0")
Java example:
int x = 5;
if (x > 0) {
System.out.println("x is positive");
} else if (x < 0) {
System.out.println("x is negative");
} else {
System.out.println("x is 0");
}
Visual Basic example:
Dim x As Integer = 5
If x > 0 Then
Console.WriteLine("x is positive")
Console.WriteLine("x is negative")
Else
Console.WriteLine("x is 0")
End If
Page 14 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
YOUR NOTES
Worked Example
Write an algorithm using pseudocode that:
Inputs 3 numbers
Outputs the largest of the three numbers
[3]
INPUT a, b, c
IF a > b AND a > c THEN PRINT a [1]
ELSE IF b > c THEN PRINT b [1]
ELSE PRINT c [1]
Page 15 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
1: PRINT "Monday"
2: PRINT "Tuesday"
3: PRINT “Wednesday”
4: PRINT “Thursday”
5: PRINT “Friday”
6: PRINT “Saturday”
7: PRINT “Sunday”
END CASE
Python example:
match (number)
case 1:
print "Monday";
case 2:
print "Tuesday";
case 3:
print "Wednesday";
case 4:
print "Thursday";
Page 16 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
case 6:
print "Saturday";
case 7:
print "Sunday";
case _:
Java example:
switch (number) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
default:
Page 17 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Case 2
Return "Tuesday"
Case 3
Return "Wednesday"
Case 4
Return "Thursday"
Case 5
Return "Friday"
Case 6
Return "Saturday"
Case 7
Return "Sunday"
Case Else
End Select
Exam Tip
Make sure to include all necessary components in the selection statement: the
condition, the statement(s) to execute when the condition is true, and any
optional statements to execute when the condition is false
Use proper indentation to make the code easier to read and understand
Be careful with the syntax of the programming language being used, as it may
differ slightly between languages
Make sure to test the selection statement with various input values to ensure
that it works as expected
Page 18 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Count-controlled Loops
A count-controlled loop is used when the number of iterations is known beforehand
It is also known as a definite loop
It uses a counter variable that is incremented or decremented after each iteration
The loop continues until the counter reaches a specific value
Example in Pseudocode:
count ← 1
FOR i <= 10
OUTPUT count
NEXT i
Example in Python:
for count in range(1, 11):
print(count)
Example in Java:
for(int count=1; count<=10; count++){
System.out.println(count);
Console.WriteLine(count)
Next count
Page 19 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
INPUT temperature
END WHILE
Example in Python:
temperature = float(input("Enter temperature: "))
Example in Java:
Scanner input = new Scanner(System.in);
temperature = input.nextFloat();
Console.WriteLine("Enter temperature")
temperature=Console.ReadLine()
Loop
Page 20 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Example in Pseudocode:
REPEAT
INPUT guess
UNTIL guess = 42
Example in Python:
Postcondition loops don’t exist in Python and would need to be restructured to a precondition
loop
Example in Java:
Scanner input = new Scanner(System.in);
int guess = 0;
do {
guess = input.nextInt();
Console.WriteLine("Enter guess")
guess = Console.ReadLine()
Page 21 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
for i ← 1 to 10
input num
next i
output total
Python example:
total = 0
total += num
print("Total:", total)
Java example:
int total = 0;
total += num;
For i As Integer = 1 To 10
Page 22 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Next i
Page 23 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
for i ← 1 to 10
input num
ount ← count + 1
end if
next i
output count
Python example:
count = 0
if num > 5:
count += 1
print("Count:", count)
Java example:
int count = 0;
if (num > 5) {
count++;
Page 24 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
count += 1
End If
Next i
Worked Example
Write an algorithm using pseudocode that:
Inputs 20 numbers
Outputs how many of these numbers are greater than 50
[3]
FOR x ← 1 TO 20 [1]
INPUT Number
Page 25 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
length ← LENGTH(phrase)
substring ← SUBSTRING(phrase, 5, 2)
upper ← TO_UPPER(phrase)
lower ← TO_LOWER(phrase)
Python example:
phrase = "Save my exams"
length = len(phrase)
substring = phrase[4:6]
Page 26 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Java example:
String phrase = "Save my exams";
Page 27 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
YOUR NOTES
Worked Example
The function Length(x) finds the length of a string x. The function substring(x,y,z)
finds a substring of x starting at position y and z characters long. The first character
in x is in position 1.
Write pseudocode statements to:
Store the string “Save my exams” in x
Find the length of the string and output it
Extract the word exams from the string and output it
[6]
X ← "Save my exams" [1] for storing string in X.
[1] for calling the function
OUTPUT Length(X) length. [1] for using the
correct parameter X.
Y ←9
Z ←5
Exam Tip
Remember that the length of a string is not the same as the index of the last
character in the string
Be careful when counting positions in a string, as the first character can be at
position zero or one depending on the programming language
Page 28 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
b←3
c←a+b
d←a-b
e←a*b
f←a/b
g ← a MOD b
h←a^b
i ← a DIV b
Python example:
a=5
b=3
c=a+b
d=a-b
e=a*b
f=a/b
g=a%b
h = a ** b
i = a // b
Java example:
int a = 5;
Page 29 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
int d = a - b;
int e = a * b;
double f = (double) a / b;
int g = a % b;
int i = a / b;
Dim b As Integer = 3
Dim c As Integer = a + b
Dim d As Integer = a - b
Dim e As Integer = a * b
Dim f As Double = a / b
Dim i As Integer = a \ b
Page 30 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
b←3
c ← (a = b)
d ← (a < b)
e ← (a <= b)
f ← (a > b)
g ← (a >= b)
h ← (a <> b)
Python example:
a=5
b=3
c = (a == b)
d = (a < b)
e = (a <= b)
f = (a > b)
g = (a >= b)
h = (a != b)
Java example:
int a = 5;
int b = 3;
boolean c = (a == b);
Page 31 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
boolean h = (a != b);
Dim b As Integer = 3
Dim c As Boolean = (a = b)
Page 32 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
END IF
END IF
IF NOT(condition) THEN
END IF
if condition1 or condition2:
if not condition:
if (condition1 || condition2) {
if (!condition) {
Page 33 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
End If
End If
End If
Exam Tip
Boolean operators are often used in conditional statements such as if, while,
and for loops
Boolean operators can be combined with comparison operators such as ==
(equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal
to), and >= (greater than or equal to)
Be careful when using the NOT operator, as it can sometimes lead to
unexpected results. It is always a good idea to test your code with different
inputs to ensure that it works as expected
Page 34 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Exam Tip
You will not be required to write more than three levels of nested statements
Page 35 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
if b > c then
else
else
if a > c then
else
Python example:
if a > b:
if b > c:
else:
else:
if a > c:
else:
Java example:
if (a > b) {
if (b > c) {
Page 36 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
} else {
if (a > c) {
} else {
If b > c Then
Else
End If
Else
If a > c Then
Else
End If
End If
Exam Tip
Indentation is key so make sure it's clear in your answer which if statement line is
part of which. It's more clear when you use end if in the appropriate places, too.
Page 37 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Pseudocode example:
FOR i ← 1 TO 10
FOR j ← 1 TO 5
END FOR
END FOR
Python example:
for i in range(1, 11):
Java example:
for (int i = 1; i <= 10; i++) {
For j As Integer = 1 To 5
Next j
Next i
Exam Tip
Nested iteration is useful when we need to perform a task for a specific range of
values
It is important to keep track of the number of nested statements
It is important to keep nested statements organized and clear. Use consistent
indentation and avoid going too many levels deep, as this can make the code
difficult to read and understand
Page 38 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Page 39 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
END PROCEDURE
Python example:
def calculate_area(length, width):
calculate_area(5,3)
Java example:
public static void calculateArea(int length, int width) {
calculateArea(5, 3);
End Sub
CalculateArea(5, 3)
Page 40 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
RETURN area
END PROCEDURE
To output the value returned from the function you would use the following pseudocode:
OUTPUT(calculate_area(5,3))
Python example:
def calculate_area(length, width):
return area
print(calculate_area(5,3))
Java example:
public static void calculateArea(int length, int width) {
return area;
System.out.println(calculateArea(5, 3));
Return area
End Sub
Page 41 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
RETURN average
END FUNCTION
total_score ← 0
END PROCEDURE
Python example:
def calculate_average(num1, num2):
return average
total_score = 0
def add_score(score):
global total_score
Java example:
static int totalScore = 0;
Page 42 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
return average;
AddScore(10)
Return average
End Function
End Sub
Page 43 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Exam Tip
Remember to import or include the appropriate library before using the routines
in your code
Page 44 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to savemyexams.com for more awesome resources
Page 45 of 45
© 2015-2023 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers