C141A284 43BA 4961 81DF 99A2264F4991 - Programming Basic For O Level
C141A284 43BA 4961 81DF 99A2264F4991 - Programming Basic For O Level
Algorithm: The steps that are required to follow to solve a computer program are known as algorithm.
Algorithm can be represented in two ways. One is Pseudocode and the other is flowchart.
Pseudocode: This resembles a programming language in high level language without following the syntax
(grammar) of a particular programming language. Some keywords are used to represent pseudocode.
Flowchart: is an alternative method of representing an algorithm. This is the graphical representation of an
algorithm. A flowchart consists of specific shapes which are linked together with flow lines.
Algorithms usually consist of three different stages. They are Input, Process and Output stages.
Input
- Is used to enter data into the system // get / receive / read data from a file
Values are input using the INPUT command as follows:
INPUT <identifier>
The identifier should be a variable
INPUT Number
Whatever the value is given by the user will be stored in variable Number.
Before taking any input it is always recommended to give a message regarding the type of value the user is
expected to give. This type of message is known as prompt. So the following example is a better way to take
input from the user.
OUTPUT “Please enter a number: ”
INPUT Number
Process
- Manipulate / change data in some way // perform a calculation / find a result
For example, MyChar ← 'X' // MyNum ← MyNum + 1
Output
- Send data out from the system // display / print / transmit / show data
For example, OUTPUT "Hello World"
Values are output using the OUTPUT command as follows:
OUTPUT <value(s)>
Several values, separated by commas, can be output using the same command.
1
Use of comments in a program
It is a good practice to use comments in a program to make the code easier for humans to understand.
Comments are not executed by the program. Comments are preceded by two forward slashes //.
Arithmetic operations
Standard arithmetic operator symbols are used:
+ addition
– subtraction
* multiplication
/ division (used for normal division)
^ raised to the power of
Examples – arithmetic operations
Answer ← Score * 100 / MaxMark
Answer ← Pi * Radius ^ 2
The integer division operators DIV and MOD can also be used.
DIV operator is used to get the integer quotient after the division of two numbers
X←15 DIV 4
The result of X will be 3
MOD operator is used to get the remainder after the division of two numbers
X←17 MOD 4
The result of X will be 1
Algorithms may be expressed using four basic constructs. They are Assignments, Sequence, Selection
and Repetition / iteration.
Selection: Under certain conditions some steps are performed, otherwise different (or no) steps are
performed. Two options are used for selection. One is IF structure and the other is CASE structure.
For example:
IF A>20
THEN
OUTPUT “The value is greater than 20”
ELSE
OUTPUT “The value is not greater than 20”
ENDIF
3
Variable: This is a name given to a memory location that can contain a value and that may change during
the execution of the program. A variable can only contain one particular type of data and one value at any
one time.
Variable declarations are required before they are used in a program.
Declarations are made as follows:
DECLARE <identifier> : <data type>
Example – variable declarations
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN
Constant: This is a name given to a memory location that can contain one value and that should not change
during the execution of the program. A constant can only contain one particular type of data.
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.
Advantages of using constants
- One change can be reflected throughout the program
- The value of the constant cannot be accidentally changed
4
Initializing a variable:
This refers to the process wherein a variable is assigned an initial value before it is used in the program.
Without initialization, if a variable is used on the right hand side of the assignment operator then this can
lead to an unpredictable output or crash the program
For example, Total ← 0 [Here variable Total has been initialized with zero]
Later on in the program this may be used as Total = Total + Number
Another example is
Count ← 0 [Here variable Count has been initialized with zero]
Count ← Count + 1 [Variable Count has been incremented by 1]