0% found this document useful (0 votes)
31 views

C141A284 43BA 4961 81DF 99A2264F4991 - Programming Basic For O Level

Uploaded by

Diganto Haque
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

C141A284 43BA 4961 81DF 99A2264F4991 - Programming Basic For O Level

Uploaded by

Diganto Haque
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Computer Science

Teacher: Maruf Ahmed

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.

Examples – INPUT and OUTPUT statements


OUPTUT “Please enter your answer: ”
INPUT Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"

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

PEMDAS rule (order of operations)


The order of operations is a rule that tells the correct sequence of steps for evaluating a math expression. We
can remember the order using PEMDAS: Parentheses, Exponents, Multiplication and Division (from left to
right), Addition and Subtraction (from left to right). It is good practice to make the order of operations in
complex expressions explicit by using parentheses.

Algorithms may be expressed using four basic constructs. They are Assignments, Sequence, Selection
and Repetition / iteration.

Assignments: A value is given to a variable


The assignment operator is ← (left pointing arrow)
Assignments should be made in the following format:
<identifier> ← <value>
The identifier must refer to a variable or a constant. The value may be any expression that evaluates to a
value of the same data type as the variable.
Example – assignments
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
For example: X ← 10, Y← “Hello world”, M←10+20-5, P←P+10, Q←0, X←10^2
In Computer Science, X ← 10 is correct but 10 ← X is not correct
2
Sequence: A number of steps are performed, one after another
For example:
X←10
Y← 20
Z←X+Y

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

Repetition/iteration/loop: A sequence of steps is performed a number of times. It may be fixed number of


times or based on certain condition.
Three different structures are used.
1. FOR....TO....NEXT [Count controlled loop]
2. WHILE....DO....ENDWHILE [pre-condition loop]
3. REPEAT....UNTIL [post-condition loop]

Basic data types used in pseudocode/programming:


Date type Description Example of literals
STRING A sequence of zero to more characters. Delimited by double quotes. A string
No mathematical calculation can be may contain zero characters (i.e. the
done. empty string)
e.g. "This is a string", ""
x ← “Hello World”
CHAR A single character. No mathematical A single character delimited by single
calculation can be done. quotes, e.g. ꞌxꞌ, ꞌcꞌ, ꞌ@ꞌ
x ← „T‟
INTEGER A whole number which can be both Written as normal in the denary
positive and negative. Mathematical system, e.g. 5, –3
calculation can be done. x ← 10
REAL A number capable of containing a Always written with at least one digit
fractional part as well as a whole on either side of the decimal point,
number. This can be both positive and zeros being added if necessary, e.g.
negative. Mathematical calculation can 4.7, 0.3, –4.0, 0.0
be done. x ← 20.25
BOOLEAN The logical values TRUE and FALSE TRUE, FALSE
x ← TRUE

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

Rules for naming an identifier (variable, constant, array etc.):


 Identifiers (the names given to variables, constants, arrays, procedures and functions) 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 identifier
 Identifiers should be considered case insensitive, for example, Countdown and CountDown should
not be used as separate variables.

Example of valid variable names: FirstName, Number1 etc.


Example of Constant: Use the word Constant before the constant name.
CONSTANT Tax←0.05, CONSTANT Discount←0.1
For a constant, it should be declared and assigned a value as follows:
CONSTANT <Identifier> ← <value>

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]

Explain the meaning of Totaling/running total/aggregate total and Counting


Totaling: This is used to add up a series or list of values of numbers.
For example, Total ← Total + Number
Counting: This is used to find out how many numbers/ items are there in a list or series of values.
For example, Count ← Count + 1

You might also like