Programming
Programming
Syllabus 2023-2025
22
23
24
String Handling
Single databases
Strings are used to store text. String handling is an important part of programming.
Length- finding the number of characters in the string. For example, the length of the
string “Computer Science” is 16 characters as spaces are counted as a character.
Substring- extracting part of a string. For example, the substring “science” could be
extracted from “computer science”
Upper- converting all the letters in a string to uppercase. For example, the string
“Computer Science” would become “COMPUTER SCIENCE”.
Lower- converting all the letters in a string to Lowercase. For example, the string
“Computer Science” would become “computer science”.
Logical operators
Boolean operators
25
Procedures and functions
When writing an algorithm, there are often similar tasks to perform that make use of the same
groups of statements. Instead of repeating these statements and writing new code every time
they are required, many programming languages make use of subroutines, also known as
named procedure or functions.
The procedure can then be called in the main part of the program as many times as is required
in the following way:
Procedure with parameters are called like this- in this case print sever stars.
26
Note: A procedure call must match the procedure definition. This means that when a
procedure is defined with parameters, the arguments in the procedure call should match the
parameters in the procedure definition.
Functions
A function is just like a procedure except it always returns a value. Unlike procedure, function
calls are not standalone and instead can be made on the right-hand side of an expression.
The keyword RETURN is used as one of the statements in a function to specify the value to
be returned. This is usually the last statement in the function definition.
Because a function returns a value, it can be called by assigning the return value directly into
a variable as follows:
A global variable can used by any part of a program- its scope covers the whole
program.
A local variable can only be used by the part of the program it has been declared in-
its scope is restricted to that part of the program.
27
For example, in this algorithm the variables Number1, Number2 and Answer are declared both
locally and globally, whereas Number3 is only declared locally.
The final line is an error because the main program has tried to access the variable Number3,
which is local to the procedure.
28
Library routines
Many programming language development systems include library routines that are ready to
incorporate into a program. These routines are fully tested and ready for use.
Arrays
An array is a data structure containing several elements of the same data type, these elements
can be accessed using the same identifier name.
The position of each element in an array is identified using the array’s index.
29
Each position in the array can be populated in an array by defining the value at each index
position. For instance, we can add the number 27 to the fourth position in the array MyList as
follows:
Notice that in this code we have used the variable Counter as the array index. We can display
the data that lies in a particular location in an array as follows:
Two-dimensional arrays
A two-dimensional array can be referred to as a table, with rows and columns. Here is an
example of a table with 10 rows and 3 columns, which contains 30 elements. The first element
is located at position 0,0
30
When a two-dimensional array is declared in pseudocode.
For example:
The declared array can be populated using a loop, just like for one-dimensional arrays-
however this time there need to be two nested loops, one for each index.
We can display the data that lies in a particular location in a two-dimensional array as follows:
31
File Handling
Computer programs store data that will be required again in a file. While any data stored in
RAM will be lost when the computer is switched off, when data is saved to a file it is stored
permanently. Data stored in a file can also be sent to be used on other computer(s). The
storage of data in files is one of the most used features of programming.
Here are examples of writing a line of text to a file and reading the line of text back from the
file.
32