Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

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”.

Arithmetic, logical and Boolean Operators

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.

Procedure, functions and parameters


A procedure is a set of programming statements grouped together under a single name that
can be called to perform task at any point in a program.
A function is a set of programming statements grouped together under a single name that can
be called to perform a task at any point in a program. In contrast to a procedure, a function
will return a value back to the main program.
Parameters are the variables that store the values of the arguments passed to a procedure or
function. Some but not all procedures and functions will have parameters.

Definition and use of procedures and functions, with or without parameters


Procedures without parameters
Here is an example of a procedure without parameters in pseudocode.

The procedure can then be called in the main part of the program as many times as is required
in the following way:

Procedures with parameters


Here is an example of how a procedure with parameters can be defined in pseudocode. We
can add parameters to a procedure.

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:

Local and global variable

 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.

There are different types of library routines.

Here are some examples of these library routines in pseudocode.

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.

One dimensional arrays

A one-dimensional array can be referred to as a list. Here is an example of a list with 10


elements in it where the first element has an index of zero.

When a one-dimensional array is declared in pseudocode:

 the name of the array


 the first index value
 the last index value
 and the data type
Are included.

For example, to declare a new array called MyList:

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:

To populate the entire array instead we can use a loop:

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:

This would display the value 19.

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.

 the first index value for rows


 the last index value for rows
 the first index value for columns
 the last index value for columns
 and the data type
are included

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:

This would display the value 98.

31
File Handling

Purpose of storing data in a file

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

You might also like