programming basic for O Level
programming basic for O Level
Pseudocode:
- Pseudocode is a simple method of showing an algorithm.
- It describes what the algorithm does by using English keywords that are very similar to those used in
a high-level programming language.
- However, pseudocode is not bound by the strict syntax rules of a programming language. It does
what its name says; it pretends to be programming code!
Flowchart:
- A flowchart shows diagrammatically the steps required to complete a task with some pre-defined
shapes (boxes) and the order that they are to be performed.
- A flowchart consists of specific shapes which are linked together with flow lines.
- It is basically a diagrammatic representation of an algorithm.
Stages of an algorithm:
- Algorithms usually consist of three different stages. They are Input stage, Process stage and Output
stage. The stages must be given in the correct sequence of Input – Process – Output.
Input stage:
- is used to enter data into the system
- is used to get / receive / read data from a file
Pseudocode example:
Values are input using the INPUT keyword as follows:
INPUT <identifier>
Most of the time the identifier is a variable
INPUT Number
Whatever the value is given by the user the value will be stored in variable Number.
Prompt: 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 input message or more precisely known as
prompt. So, the following example is a better way to take input from the user.
Pseudocode example:
OUTPUT “Please enter a number: ”
INPUT Number
Process stage:
- is used to manipulate / change data in some way
- is used to perform a calculation / find a result
Pseudocode example:
MyChar ← 'X' [Storing a CHAR type value in MyChar]
MyNum ← MyNum + 1 [Updating the previous value of MyNum by adding 1 to it]
Page 1 of 9
Process is always represented with Left pointing array (←) in pseudocode.
Output stage:
- is used to send data out from the system
- is used to display / print / transmit / show data / write data to a file
Pseudocode example:
OUTPUT "Hello World"
Message can be displayed using OUTPUT keyword
Result of a variable can be output using a message or without a message
For example:
OUTPUT Total [Displayed without any message, it will only show the value that will be held in variable
Total]
OUPUT “The total is: ”, Total [Displayed with a message]
N.B. When you want to display the result of a variable, it must not be in any quotation mark.
You have to understand that every OUTPUT keyword will give a new line on screen. Several values,
separated by commas, can be output using the same keyword.
For example, OUTPUT “The total of ”, count, “numbers is: ”, Total
N.B. A comma (,) is used as a separator between variables, and also between a message and a variable.
Keywords:
- Keywords, also known as reserved words, are a type of predefined tokens in a programming
language that have special meanings and purposes
- These words are reserved by the language and cannot be used as variable names or any other
identifiers
- In pseudocode, keywords have the same use as in any programming language. It is highly
recommended that all the keywords are written in all capital letters in pseudocode
Basic constructs needed in developing an algorithm: Algorithms may be expressed using five basic
constructs. They are:
• Data stores / Data use
• Sequence
• Selection
• Iteration
• Use of operators
Page 2 of 9
Sequence:
- A number of steps are performed, one after another
Pseudocode example:
OUTPUT “Enter your age: ”
INPUT Age
OUTPUT “Your age is:”, Age
In the above example, statements will be executed in the order that has been written.
Selection:
- In a selection statement, the path is decided depending on the result of a condition
- Under certain conditions some steps are performed, otherwise different (or no) steps are performed.
- Two structures are used for selection. One is IF structure and the other is CASE structure.
IF structure example:
IF A>20
THEN
OUTPUT “The value is greater than 20”
ELSE
OUTPUT “The value is not greater than 20”
ENDIF
Repetition/iteration:
- A sequence of steps is performed may be for a number of times or may be for an unknown number of
times.
- If it is for a known number of times then count-controlled loop is used and if it is for an unknown
number of times then a condition-controlled loop is used.
Use of operators:
- All programming languages make use of different operators to perform different operations
Page 3 of 9
/ (forward slash for normal division where the quotient may be in whole number or in decimal)
^ (Caret for raised to the power of / exponent)
Examples – arithmetic operations
Answer ← Score * 100 / MaxMark
Answer ← Pi * Radius ^ 2
DIV and MOD operators (these are also known as arithmetic operators):
The integer division operators DIV and MOD can also be used. Both the operators will give the result in
whole numbers only. In both cases the first number is divided by the second number.
DIV: DIV operator is used to get the integer quotient after the division of first number by the second
number
X←15 DIV 4
The result of X will be 3, as 4 goes into 15, 3 times as a whole
MOD: MOD operator is used to get the integer remainder after the division of first number by the second
number
X←17 MOD 4
The result of X will be 1 as 4 goes into 17, 4 times as a whole and the integer remainder is 1
Use of Logical operators: These are used to decide which path to take through a program by evaluating the
expression as true or false. This is used in comparisons/conditional statements/selection statements. The
logical operators are: Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to
(<=), Is equal to (=) and Not equal to (< >)
For example: IF A > B
Use of Boolean operators: These are used for operations with true or false values and the result is also
either true or false. NOT, AND and OR are known as Boolean operators
For example: IF A>20 AND B>30
Pseudocode example:
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
Y← “Hello world”
P ← P+10
Page 4 of 9
X ← 10^2
In Computer Science, X ← 10 is correct but 10 ← X is not correct. That means a literal or an expression can
be to the right side of the assignment operator and not to the left side.
Data type: A data type classifies how the data is stored, displayed and the operations that can be performed
on the stored value. For example, a variable with an integer data type is stored and displayed as a whole
number and the value stored can be used in arithmetic calculation. You cannot use this integer variable for
storing any other type of values such as of STRING or REAL data type.
For each variable you are going to use in the program, the data type for that variable must be identified. The
identifier (name) along with the data type needs to be declared before you use in the program. You cannot
use the same variable for holding two different data type values.
Variable:
- 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.
- The same variable cannot be used to store a different data type value in the same program. That
means if a variable has been declared as integer, then it can be used to store only whole number
throughout the program and no value of any other data type
- Variable can be used to take input in a program
Page 5 of 9
Variable declarations are normally required before they are used in a program.
Declarations are made as follows:
DECLARE <identifier> : <data type>
Example of variable declarations
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN
If any program requires multiple variables of the same data type to be used, then they can be declared
together separated by comma.
For example:
DECLARE Num1, Num2, Result : INTEGER
Constant:
- A name given to a memory location that can contain one value and that cannot change during the
execution of the program
- A constant can only contain one particular type of data
- There is no way a constant value should be changed during the execution of the program
- Constant cannot be used to take input in a program
When we know that the value will not be changed during the execution of the program then we use constant.
Constant cannot be used to take input. It can only be used to assign a literal value before use.
Constants are declared by stating the identifier and assigning the literal value in the following format:
CONSTANT <identifier> ← <value>
Example of constant declarations: Use the word CONSTANT before the constant name.
CONSTANT HourlyRate ← 6.50 [Referring to a REAL data type value]
CONSTANT DefaultText ← "N/A" [Referring to a STRING data type value]
CONSTANT Discount ← 10 [Referring to an INTEGER data type value]
N.B. No two constant can be declared together. Each constant must be declared separately. Only literals can
be used as the value of a constant. A variable, another constant or an expression must not be used.
Explain how variables and constants should be used when creating and running a program.
• variables and constants should have meaningful identifiers so that programmers / future
programmers are able to understand their purpose
• they are both used for data storage
• constants store values that never change during the execution of a program
• variables contain values that have been calculated within the program / can change during the
execution of the program
Page 6 of 9
Rules for naming an identifier (names given for variables, constants, arrays, subroutines etc.):
• There cannot be any space between words in any identifier
• Identifiers are in mixed case using Pascal case (PascalCase is a naming convention in which the first
letter of each word in a compound word is capitalized.), e.g., FirstName.
• They can only contain letters (A–Z, a–z) and digits (0–9).
• They must start with a capital letter and not a digit. Accented letters and other characters, including
the underscore, should not be used.
• As in programming, it is good practice to use identifier names that describe the variable, procedure
or function to which they refer.
• Keywords should never be used as identifier names. For example, INPUT, OUTPUT, IF, FOR etc.
should not be used as identifiers
• Identifiers should be considered case insensitive, for example, Countdown and COUTNDOWN
should not be used as separate variables.
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 [The previous Total has been added to
Number to update the new Total]
Another example is:
Count ← 0 [Here variable Count has been initialized with zero]
Count ← Count + 1 [Variable Count has been incremented by 1 later on in the program]
But without initializing Count ← 0, if Count ← Count + 1 statement is used then the program will try to add
a value of Count with 1, but the variable does not have any value assigned to it. So, there will be an error as
Count has not been defined.
Solution:
CONSTANT PersonName ← “Bill Gates”
OUTPUT “Your name is ”, PersonName
Problem: Write down the pseudocode that will take a name as an input from a user and displays it with a
proper message.
Solution:
DECLARE PersonName: STRING
OUTPUT “Please enter your name: ”
INPUT PersonName
OUTPUT “Your name is ”, PersonName
Problem: Write down the pseudocode that will store two whole numbers 20 and 50 in two constants and
calculate the total and display the result.
Page 7 of 9
Solution:
DECLARE Addition : INTEGER
CONSTANT Num1 ← 20
CONSTANT Num2 ← 50
Addition ← Num1 + Num2
OUTPUT “The total of ”, Num1, “and ”, Num2, “is ”, Addition
Problem: Write down the pseudocode that will take two numbers as input and display the total with a
proper message.
Solution:
DECLARE Num1, Num2, Result: REAL
OUTPUT “Please enter first number: ”
INPUT Num1
OUTPUT “Please enter second number: ”
INPUT Num2
Result ← Num1 + Num2
OUTPUT “The total of ”, Num1, “and ”, Num2, “is ”, Result
Alternate way (multiple inputs can be taken together):
DECLARE Num1, Num2, Result: REAL
OUTPUT “Please enter two numbers: ”
INPUT Num1, Num2
Result ← Num1 + Num2
OUTPUT “The total of ”, Num1, “and ”, Num2, “is ”, Result
Problem: Write down the pseudocode that will take input of a name of a student and the mark achieved by
the student and displays the name and the mark together with a proper message.
Solution:
DECLARE SName : STRING
DECLARE SMark : INTEGER
OUTPUT “Please enter the student name: ”
INPUT SName
OUTPUT “Please enter the subject mark: ”
INPUT SMark
OUTPUT “The name of the student is ”, SName, “and the mark achieved by the student is ”, SMark
Problem: Write down the pseudocode that will take input of number of eggs that can be distributed evenly
in 5 baskets. The program will calculate how many eggs as a whole number can be kept in each basket and
how many eggs as a whole number cannot be kept in any basket.
Solution:
DECLARE NumberEggs, EachBasketEgg, LeftEggs : INTEGER
OUTPUT “Enter number of eggs to distribute:”
INPUT NumberEggs
EachBasketEgg ← NumberEggs DIV 5
OUTPUT “Each basket can have ”, EachBasketEgg, “eggs”
LeftEggs ← NumberEggs MOD 5
OUTPUT “Number of eggs cannot be in any basket is ”, LeftEggs, “eggs”
Page 8 of 9
Symbols / Shapes used to represent an algorithm using flowchart
For drawing flowchart to represent an algorithm, the following Symbols / shapes are used. Variable
declarations will never be included in flowchart.
Page 9 of 9