Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
268 views

Lesson Notes Programming

This document discusses programming languages and the program development process. It covers the different generations of programming languages from machine languages and assembly languages as low-level first and second generation languages, to high-level third, fourth and fifth generation languages like COBOL, Pascal, FORTRAN, BASIC and Visual Basic. It also discusses key concepts in program development like algorithms, pseudocode, flowcharts, variables, constants, identifiers, syntax, loading, debugging, test data, and the different types of errors. The development process involves defining the problem, analyzing it, and developing an algorithm, flowchart or pseudocode to represent the solution.

Uploaded by

craig burey
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
268 views

Lesson Notes Programming

This document discusses programming languages and the program development process. It covers the different generations of programming languages from machine languages and assembly languages as low-level first and second generation languages, to high-level third, fourth and fifth generation languages like COBOL, Pascal, FORTRAN, BASIC and Visual Basic. It also discusses key concepts in program development like algorithms, pseudocode, flowcharts, variables, constants, identifiers, syntax, loading, debugging, test data, and the different types of errors. The development process involves defining the problem, analyzing it, and developing an algorithm, flowchart or pseudocode to represent the solution.

Uploaded by

craig burey
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

LESSON 1

TOPIC: PROGRAMMING LANGUAGES


Program
A computer program is a set of computer
instructions which are used for solving a
problem. The program directs the computer to
perform the actions needed to arrive at a
solution.

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.

Low level languages


Programs written using low level languages
are machine-dependent. A machine-dependent
language only runs on one particular computer.
These programs are not portable to other
computers. The programmer needs to have
detailed knowledge of the operation of various
hardware components of the computer.

First generation languages and Second


generation languages are low level.

First Generation Languages (1GLs)


1940s
Machine Languages

It consists of a series of 0s and 1s (binary


digits).
Machine language programs are the fastest
programs when being executed.
Machine language programs are machinedependent. That is, programs written on
one type of computer cannot be transferred
in the same form to another type of
computer.

Example of Machine Language program


Machine Code Program

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

Advantage of using Machine Language


1. Machine language programs are the fastest
programs when being executed. They
have fast execution speed because they
require no translation from one language to
another.

Disadvantage of using Machine


Language
1. It is time consuming and tedious to
write.
2. It is difficult to find and correct
programming errors.
3. It is machine-dependent. This means that
programs written for one type of computer
will not work on another type of computer.

Second Generation Language (2GLs)


Early 1950s
Assembly Language

These programs use short codes


(mnemonics) to represent instructions.
Translator programs called assemblers
were developed to convert assembly
language programs to machine language.
Assembly language programs are also
machine-dependent. That is, programs
written on one type of computer cannot be
transferred in the same form to another
type of computer.

Example of Assembly Language


program
mov ax 5
add ax, 10
mov ax, [bx]
xor ax, 67
jmp [bx]
sub [bx], 8

Advantages of using Assembly Language


1. Programming is faster and less tedious
than for machine language.
2. Code is easier to learn, read and
understand than for machine language.

Disadvantage of using Assembly


Language
1. Programming is slower and more
tedious than for high level languages.
2. It is machine-dependent. This means
that programs written for one type of
computer will not work on another type
of computer.

High level Languages


Programs written using high level languages
are machine-independent. A machineindependent language can run on many
different types of computers and operating
systems with little or no modification. A
programmer using a high level language is not
required to have detailed knowledge of the
operation of various hardware components of
the computer.

Instead of using binary digits or mnemonics to


produce program instructions, high level
languages use arithmetic symbols
(such as +, *) and human-like words to
simplify the program development process for
the programmer.

Advantage of using High Level


Languages
1. Programming is faster and less tedious.
2. Code is easier to learn, read and
understand.
3. Programs written in high level
languages use English-like statements
making then easier to write.
4. It is machine-independent. This means
that programs written for one type of
computer will work on another type of
computer.

Disadvantage of using High Level


Languages
1. Programs written in this language
execute slower.

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.

Third generation, Fourth generation and


Fifth generation languages are high level.

Third Generation Languages (3GLs) Late 1950s and 1960s


Examples of these languages are:
Common Business Oriented Language
(COBOL)

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

Formula Translation (FORTRAN)


Purpose
To solve problems that involve complex
mathematics.

Beginners All-purpose Symbolic


Instruction Code (BASIC)

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.

Fourth Generation Languages (4GLs)


Late 1970s
TRANSLATORS
Fourth Generation Languages (4GLs) are
easier to write than any of the earlier
generation languages, they use statements that
are close to human language.

Fifth Generation Languages (5GLs)


Late 1990s
The fifth generation languages are
programming languages that contain visual
tools to help develop a program. A good
example of a fifth generation language is
Visual Basic.

Natural Language programming


This is an approach to programming that
would enable you to use expressions very
much like your own native language. Such a
programming language would have to be able
to recognize a wide range of command words,
resembling those used in normal language.
One of the drawbacks of natural language
programming is that the number of command
words has to be large enough to accommodate
the range of expressions used in the human
language. The system may even need to learn
expressions that are used on a regular basis, to
keep up with new words. As the power of the
computer increases, the possibility of using a
real natural language for programming will
grow.

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

Dry run testing

This is a language consisting of English-like


statements used to define the steps of an
algorithm.

A dry-run test involves applying test data to


the program manually and tracing the logic or
reasoning of the program. Trace tables are
tools which are very helpful during dry runs.

Flowchart

Trace table

This is a design tool used to graphically


represent the logics in an algorithm solution by
using symbols to represent various operations.

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.

A trace table is a table that is completed by


tracing the instructions in an algorithm with
given data to arrive at solutions.

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

6. Document how the program works and


how to use it.

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.

Variable Name Checklist

The name must not be too long.


The name must be descriptive of the data
being stored.
There should be no space between variable
names consisting of two words. For
example a variable name to store a unit
cost can be written as UnitCost or
Unit_Cost. A space between the two
words is a syntax error.
The variable name must not begin with
numbers or special characters e.g. *, #, &.
Variable names should be consistent
throughout the pseudocode algorithm.
That is a variable name chosen at the start
of a pseudocode algorithm should remain
the same from start to finish.

Write a pseudocode algorithm to read two


numbers into variable A and B.

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

Rvalue as the result of an expression


Sum = Num1 + Num2
Lvalue

Layout
Read Variable Name
or
Input Variable Name

Rvalue as the contents of another variable


Num1 = Num2

Practice Question

Practice Question

Lvalue

Write a pseudocode algorithm to read two


numbers and find their sum.

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.

CLASS WORK1 INPUT STATEMENT


1.
2.
3.
4.
5.
6.

Write an instruction to input a number.


Write an instruction to read the name of a student.
Write an instruction to input two numbers.
Write an instruction to input the name, age and height of a student.
Write an instruction to input the quantity and price of an item.
Write an instruction to read three scores.

CLASS WORK2 OUTPUT STATEMENT AND PROMPTS


Exercise 20
Information Technology to programming, Chapter 20 page 75
Questions 1-10

Practice Exercise

PROMPTS

Explanation

While entering data, messages may appear on


the screen notifying the user of what data to
be entered. These messages are called prompts
or captions. Its is good programming practice
to include a prompt or a caption for each input.
Prompt statements begin with the command
PRINT followed by the message enclosed in
quotations marks. All messages should be
proper messages which are written in English.
Meaningful abbreviations may be used.
For Example, PRINT Enter the name, name

Line 4 This instruction will display the


message The sum is as well as the contents
stored in the variable Sum.

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

Print Variable Name


or
Write Variable Name

Division

Multiplication

Layout2

Equal to

<>

Not equal to

>

Greater than

Write a pseudocode algorithm to read two


numbers and find their sum. Print the results.

>

Less than

>=

Greater than or equal to

Solution1

<=

Less than or equal to

The output statement is used to get information


to the programmer or computer user.

Print Message, Variable Name


or
Write Message, Variable Name

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.

Choosing the correct if-statement to


solve the problem
A number of if-statements are available for use
in programming. Each type of if-statement
will perform differently; so it is very important
to know what results you are typing to
accomplish.

THE IF-THEN STATEMENT


When one option is available and a selection
may or may not be made, the if-then statement
is used.

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.

THE IF-THEN-ELSE STATEMENT


When two options are available and a selection
must be made, use the if-then-else 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

Line 3 The message Old person will be


displayed to the user if the result obtained from
Line 2 is true (that is the value stored in the
variable Age is greater than 35).

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

Line 4 This instruction allows another


instruction to be carried out it the results
obtained from Line 2 are false.
Line 5 The message Young person will be
displayed to the user if the result obtained from
Line 2 is false (that is the value stored in the
variable Age is not greater than 35).

THE IF-THEN-ELSE-IF STATEMENT


When two or more options are available and a
selection may or may not be made, use the ifthen-else-if statement.

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.

While Stomach not full do

For CONTROLVARIABLE = 1 to N do
Statements
endfor

Illustration of The For Loop using


the Cornflakes Example

Block of instructions
carried out for an
indefinite number of
times

Fill spoon with cornflakes


Put cornflakes in mouth
Chew cornflakes
Swallow
endwhile

Layout of The For Loop

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

Illustration of The While Loop using


the Cornflakes Example

NOTE:
Use the For loop when you have a block of
statements that will be carried out a set
number of times.

Use the While loop when you do not know


exactly how many times the block of statements
will be carried out. In this case there is some
terminating conditions.

Four Major Elements of Loop


Statements

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

We need a statement that will instruct the


computer to repeat instructions. In the
cornflakes examples, these statements
were the for endfor and the
while endwhile statements.

Layout of the While Loop

Loop Block

While CONDITION do
Statement(s)
endwhile

Conclusion

We must specify what statements are to be


repeated.

When a loop is over we may need to


perform some tasks. For example, we may
need to print the results of calculations.
However, not all loops need conclusion
statements.

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.

Example of how this works


Add 5, 6, 6, 9 and 1.
0+5=5
5 + 6 = 11
11 + 4 = 15
15 + 9 = 24
24 + 1 = 25

(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

The cumulative total is stored in a variable.


The variable is assigned a value of 0 and as
each value is added, a new cumulative total
results.

Layout
cumulative variable = 0
cumulative variable = cumulative variable + variable

THINGS TO CONSIDER WHEN


CONSTRUCTING A SOLUTION TO A
PROGRAMMING QUESTION THAT
CONTAINS A LOOP
1. What type of loop do I need? Definite or
indefinite?
2. Does the question require any input? Yes
or no? If the answer is yes then for a for
loop your first input statement must take
place somewhere immediately following
the beginning of the loop. If your loop is a
while loop, then you will have to use an
input statement before the beginning of the
loop and towards the end of the loop.
3. Do I need to count anything? If yes, how
many? For each item to be counted you
need to initialize each counter variable
before the start of the loop and you need to
put each counter construct inside the loop.
4. Do I need to sum or accumulate any value?
If yes, how many? For each value to be
accumulated you need to initialize an
accumulator to 0 outside the loop and you
need to place an accumulator construct
inside the loop.
5. Is there any condition or conditions
specified by the question? If yes, each
counter, accumulator or print statement
within the loop block, ask yourself under
what condition does it do this?
6. Do I need to print anything? If yes, where
do I put my print statement? Inside the
loop or outside the loop? A print statement
placed inside the loop will be executed
each time the loop block is repeated.
7. What action must I perform when the loop
terminates? This depends on what is asked
by the question.

You might also like