Lesson Notes Programming
Lesson Notes Programming
Programmer
A computer programmer writes the instructions
that are used for solving the
problem.
There are two types of programmers:
Application Programmer and System
Programmer.
Programming
This is the process of creating and modifying
programs that guide computers.
Programming Language
A programming language is a set of words,
symbols and codes used to create instructions
that a computer can understand or recognize.
GENERATIONS OF PROGRAMMING
LANGUAGES
Programming languages are classified as either
low level or high level.
Explanation
000011010100101010000000Load the
contents of
memory location
E00 into the
accumulator.
01101001
Clear the carry
flag.
0010101001000000
Add 32 to the
content of the
accumulator
000001010100101010000000
Store the
result back in
memory
location E00
Source code
This is a program written in high level
language. The source code program must be
converted to machine language before the
computer can understand it.
A compiler or an interpreter is used to perform
this translation.
Object code
This is the machine language version that
results from compiling the source code.
Pascal
Purpose
To solve business problems such as inventory
control, accounts and any problems involving
processing large volumes of data.
Purpose
To assist in the teaching of programming
concepts but it can also be used for solving
business problems.
Sample Code:
Open input data in
Output answer out
Read data in
Add number 1 in, number 2 in giving sum - out
Sample Code:
Program Add-It (Input, Output);
Var Num1, Num2, Sum: Integer;
Begin
Readln Num1, Num2;
Sum = Num1 + Num2;
End
Sample Code:
INTEGER NUM1, NUM2, SUM
READ (5, 10) NUM1, NUM2
10 FORMAT (14,14)
SUM = NUM1 + NUM2
Purpose
To solve mathematical problems and is used
today for solving business problems.
Sample Code:
10 INPUT A
20 INPUT B
30 SUM = A + B
40 PRINT SUM
C Programming
Purpose
To write operating system programs and
business software.
Sample Code:
#INCLUDE <STDIO.H>
MAIN ( )
{
INT APPLES, PEARS;
APPLES = 5;
PEARS = 6;
SUM = APPLES + PEARS;
}
These languages use statements that are close to human language and are therefore easier to write.
Compiler
This is a program that translates the entire
program at once, then execution of the
instruction take place.
Advantages of a Compiler
1. A compiled program will almost
always run faster than an interpreted
one because no translation is taking
place at the same time.
2. The object code (the machine code
generated by the compiler) may be
saved on a disk and can be run
whenever required without being
recompiled. The user does not a
compiler to do this.
Interpreter
This is a program that translates each
instruction one at a time, executing each
instruction immediately after translation.
Advantage of an Interpreter
1. Interpreters are very convenient for
program development because making
modifications does not mean that the
entire program has to be recompiled.
Many interpreters will allow a program
to run up to the point where an error
occurred and allow the programmer to
fix the error and continue from that
point.
LESSON 2
TOPIC: PROGRAMMING DEVELOPMENT
TERMS ASSOCIATED WITH PROGRAM DEVELOPMENT
Algorithm
This is a clear arrangement of steps needed to
solve a problem.
Pseudocode
Flowchart
Trace table
Variable
This is a storage location for storing data that
may change during the processing of the
computer program.
Constant
This provides locations for storing data which
do no change value during the execution of a
program.
Identifier
This is the name invented by a programmer for
a data item. An identifier can be the name of a
variable or a constant.
Syntax
This is the set of grammar or rules that specify
how the program instructions are to be written.
Loading
For a program to run, it must be read from the
disk into memory. This is called loading.
Debugging
This means removing (correcting) errors that
are found to exist in the program.
Test data
This is data that is used to test a program for
logic errors.
Logic error
This type of error occurs when the programmer
fails to properly think through the solution to
the problem.
Syntax error
This type of error occurs when the programmer
does not adequately know the rules of the
programming language.
Executing
This is when the instructions in a program get
carried out.
PROGRAM DEVELOPMENT
ACTIVITIES
1. Define the problem
2. Analyze the problem
3. Develop an algorithm to perform these
steps and test it.
4. Write the computer program
5. Test and debug the program
LESSON 3
TOPIC: WRITING PSEUDOCODE ALGORITHMS
INPUT, ASSIGNMENT AND OUTPUT STATEMENTS
PSEUDOCODE STATEMENTS
Pseudocode statements are made up of four
kinds of statements. These are input,
processing, storage and output statements.
Each type of statement has its own purpose
and can be used more than once within the
same algorithm. Each statement begins with a
command or a variable.
INPUT STATEMENT
The input statement is used to get data from
outside the computer via some input device
into a variable.
Variable
This is a storage location for storing data that
may change during the processing of the
computer program.
Solution
Read A
Read B
(Line 1)
(Line 2)
Explanation
Line 1 - This instruction allows a user to enter
a value that will be stored in a variable called
A.
Line 2 - This instruction allows a user to enter
a value that will be stored in a variable called
B.
ASSIGNMENT STATEMENT
Assignment statements are used to give initial
value to variables and to change the value
assigned to a variable.
Layout
Lvalue = Rvalue
Lvalue refers to the variable as the storage
location. Rvalue can be a value, the result of
an expression or the contents of another
variable.
Example
Rvalue as a value
Num1 = 6
Lvalue
Layout
Read Variable Name
or
Input Variable Name
Practice Question
Practice Question
Lvalue
Solution
Read Num1
(Line 1)
Read Num2
(Line 2)
Sum = Num1 + Num2 (Line 3)
Explanation
Line 3 This instruction will add the value
stored in the variable Num1 to the value stored
in the variable Num2. The result of the
addition will be stored in a variable called
Sum.
Practice Question
Write a pseudocode algorithm to read two
numbers and find their product.
Practice Question
Write a pseudocode algorithm to read three
numbers and find their average.
Practice Exercise
PROMPTS
Explanation
OUTPUT STATEMENT
OPERATORS USED IN
PSEUDOCODE ALGORITHMS
The following operators are used when
developing pseudocode algorithms that require
calculations and comparisons.
Operator
Explanation
Addition
Layout1
Subtraction
Division
Multiplication
Layout2
Equal to
<>
Not equal to
>
Greater than
>
Less than
>=
Solution1
<=
Practice Question
Read Num1
Read Num2
Sum = Num1 + Num2
Print Sum
(Line 1)
(Line 2)
(Line 3)
(Line 4)
Explanation
Line 4 This instruction will display the
contents stored in the variable Sum.
Solution2
Read Num1
(Line 1)
Read Num2
(Line 2)
Sum = Num1 + Num2 (Line 3)
Print The sum is, Sum (Line 4)
LESSON 3
TOPIC: WRITING PSEUDOCODE ALGORITHMS
CONDITIONAL STATEMENTS
CONDITIONAL STATEMENTS
When a program is executed, each instruction
is processed in the sequence listed in the
program, unless specific instructions direct it
to deviate and select other instructions.
THE IF-STATEMENT
The if-statement is a conditional statement and
it allows deviation and selection to take place.
Layout
If <condition> then
<one or more instructions which will be
carried out if the condition is true>
Endif
The instructions between the if-then and endif
are executed only if the condition is true. If
the comparison is false, the instructions
between the if-then and endif are ignored.
Practice Question
Read the time. If the time is 11:00, output
Ring the bell.
Solution
Read Time
(Line 1)
If Time = 11 then
(Line 2)
Print Ring the bell(Line 3)
Endif
(Line 4)
Explanation
Line 2 This instruction checks the value
stored in the variable Time to see if it contains
the value 11.
Line 3 The message Ring the bell will be
displayed to the user if the result obtained from
Line 2 is true (that is the variable Time
contains the value 11). If the result obtained
from Line 2 is not true nothing will be
displayed.
Line 4 This instruction terminates the if-then
statement.
Layout
If <condition> then
<one or more instructions which will be
carried out if the condition is true>
Else
<one or more instructions which will be
carried out if the condition is false>
Endif
Practice Question
Input the age of a person. If the age is greater
than 35, output Old person otherwise output
Young person.
Solution
Read Age
If Age > 35 then
Print Old person
Else
Print Young person
Endif
(Line 1)
(Line 2)
(Line 3)
(Line 4)
(Line 5)
(Line 6)
Explanation
Solution
Read Stand
Read Spectators
If Stand = A then
Revenue = Spectators * 2.00
Else
If Stand = B then
Revenue = Spectators * 2.50
Else
If Stand = C then
Revenue = Spectators * 4.00
Else
If Stand = D then
Revenue = Spectators * 5.00
Endif
Endif
Endif
Endif
Print Stand, Stand
Print Revenue, Revenue
Layout
If <condition> then
<one or more instructions>
Else
If <condition> then
<one or more instructions>
Else
If <condition> then
<one or more instructions>
Endif
Endif
Endif
Note that, for every if-statement, there
must be a corresponding Endif.
Practice Question
A stadium has four stands A, B, C, D. The
admission fee for stand A is $2.00, stand B is
$2.50, stand C is $4.00 and stand D is $5.00.
Read a stand and the number of spectators in
the stand. Calculate and print the revenue for
the stand.
LESSON 4
TOPIC: WRITING PSEUDOCODE ALGORITHMS
LOOP STATEMENTS
The For Loop
It is a definite loop.
It repeats a block of instructions a definite
number of times.
For CONTROLVARIABLE = 1 to N do
Statements
endfor
Block of instructions
carried out for an
indefinite number of
times
NOTE:
Block of instructions
carried out exactly ten
times
For Counter = 1 to 10 do
Fill spoon with cornflakes
Put cornflakes in mouth
Chew cornflakes
Swallow
endfor
NOTE:
Use the For loop when you have a block of
statements that will be carried out a set
number of times.
Initialization
Before a loop is started we may need some
statements to get started. For example, we
may need to initialize a variable to a start
value.
Repetitive Statements
The While Loop
It is an indefinite loop
It repeats a block of instructions until a
certain condition is met
Loop Block
While CONDITION do
Statement(s)
endwhile
Conclusion
COUNTING
Counting involves increasing the value by a
fixed amount repeatedly.
The counter variable must be given an initial
value before the counter instruction is carried
out so as to facilitate the calculation. The
initial value for the counter variable is usually
0.
Layout
counter variable = counter variable + 1
Practice question
Read a number. Count the number. Print the
count.
Solution
Count = 0
Read Num
Count = Count + 1
Print Count
NOTE:
Variable names
for counters
should be chosen
to reflect what is
being counted.
TOTALLING
The cumulative total is a progressive total that
is arrived at by adding a value to the current
total to obtain a new total.
(5 is added to 0 to obtain 5)
(6 is added to 5 to obtain 11)
(4 is added to 11 to obtain 15)
(9 is added to 15 to obtain 24)
(1 is added to 24 to obtain 25
Layout
cumulative variable = 0
cumulative variable = cumulative variable + variable