PROGRAM
IMPLEMENTATION
2
Computer Programs
A computer program is a set of instructions that
tell a computer what to do and how to perform a
task.
Programming languages, like human languages,
consist of a grammar and a set of rules that
govern how elements of the grammar combine to
form instructions.
The set of rules is called the syntax and must be
strictly followed for programming instructions to
be valid
3
Categories of Programming Languages
Low-Level Language
High-Level Language
The History of Programming Languages
https://www.youtube.com/watch?v=mhpslN-OD_o
4
Low-Level Language
These languages are machine dependent.
That is, the code written can only be
understood by the computer or processor
that was used to write the code.
Example:
First generation language
Second generation language
5
First Generation Languages (1GL)
First generation languages which is also called Machine
Language, consists of strings made up of 1s and 0s.
Advantages of 1GL
Fastest to execute because it is already in the
language that the computer understands.
Uses the processor and memory more efficiently
Disadvantages of 1GL
Difficult to decipher
Machine dependent
Time consuming/ tedious to write
6
Second Generation Languages 2GL
Second generation languages, also known as Assembly Language is
written using mnemonic codes. That is, short codes that suggest their
meaning. These codes represent operations, addresses that relate to
main memory and storage registers of the computer. Typical codes
might be: LDA, STO, ADD, etc.
7
High-Level Language
Example :
These languages are machine
independent. That is, programs
written on one computer can Third generation language
generally be used on another Fourth generation language
similar computer. They also use Fifth generation language
keywords similar to English and
are easier to write.
8
Third Generation Languages 3GL
Third generation languages or High-Level Languages have a grammar and
syntax that closely resemble human languages and mathematical notations.
3GLs are closer to human language and is designed to be more portable and
less tied to the underlying hardware than lower-level languages like assembly
language or machine code.
Example:
Pascal Advantages of 3GL
• It uses English words and symbols and is
BASIC – Beginner’s All-purpose Symbolic Instruction Code
therefore even easier to write.
C • It is machine independent.
Disadvantages of 3GL
FORTRAN
• Execution is slower than low level
COBOL languages
• Requires translation
Python
• May take up more storage, in the case of
a compiled program.
9
Fourth Generation Languages 4GL
Fourth Generation Programming Languages use statements like those
used in human language. These languages are often used for database
management, report generation, and other application-specific tasks.
Database query languages, e.g., SQL (Structured Query Language)
Examples: Report generators, e.g., Oracle Report
GUI creators, e.g., Visual Basic
10
Fifth Generation Programming Languages 5GL
A Fifth Generation Language is programming using a visual or
graphical development interface to create source languages that
are usually compiled with a 4GL language compiler. 5GLs are
designed to build specific programs that help the computer solve
specific problems. They are essentially 4GLs with a knowledge
base. 5GLs are often used in Artificial Intelligence (AI). 5GLs
increases automation, and natural language processing.
5GLs were envisioned to allow non-programmers to specify their
requirements in a more human-like manner, thereby making
software development more accessible.
Examples:
OPS5 (Official Production System)
Prolog (Logical programming)
Mercury
11
Translating Code
ALL CODE MUST BE TURNED INTO MACHINE CODE! 101101010101001
IS THE ONLY THING THE COMPUTER UNDERSTANDS.
12
A Translator Program
A translator is a programming language processor that converts a computer
program from one language to another. It takes a program written in source
code and converts it into machine code. It discovers and identifies the error
during translation.
Examples of Translator Programs:
• Assembler
• Interpreter
• Compiler
13
Machine Code
Machine code is the lowest level of programming language because the instructions are
executed directly by a computer’s central processing unit (CPU). It is important to
understand that every CPU or CPU family has its own machine code instruction set.
Machine code instructions are simply numbers stored as a binary pattern of
bits i.e., 1010100101000000.
Because machine code instructions are the only ones the CPU can execute, the source code
for ALL other programming languages must be converted into machine code before it can
be executed. This translation is carried out by special programs
called compilers, translators/interpreters or assemblers.
14
Types of Translators
There are 3 different types of translators as
follows:
Compiler
Interpreter
Assembler
A compiler is a translator used to convert
high-level programming language to low-
level programming language. It converts
the whole program in one session and
Compiler reports errors detected after the
conversion. The compiler takes time to
do its work as it translates high-level
code to lower-level code all at once and
then saves it to memory.
15
Just like a compiler, is a translator used
to convert high-level programming
language to low-level programming
language. It converts the program one
line at a time and reports errors
Interpreter detected at once while doing the
conversion. With this, it is easier to
detect errors than in a compiler. An
interpreter is faster than a compiler as it
immediately executes the code upon
reading the code.
16
An assembler is a translator used to
translate assembly language to
machine language. It is like a
compiler for the assembly language.
An assembler translates a low-level
language, an assembly language to an
even lower-level language, which is
Assembler the machine code. The machine code
can be directly understood by the
CPU.
READ:
https://teachcomputerscience.com/pro
gramming-languages/
17
18
The Steps of Programming
Implementation
19
The Steps of Programming Implementation
There are several steps that must be completed before a program can be put in
use by a user.
Source Code Writing
The procedures must then be converted into the syntax of a suitable programming
language. This “typed up” version of the code or procedures is known as the SOURCE
CODE.
Compilation
the compiler scans the source code for syntax errors. If there is no error, the code is
made into an executable file. The result of this phase is called OBJECT CODE.
Linking
Linking is a process that takes place when
more than one object code is needed to
create an executable code. It creates a link
among object codes after the completion of
the compilation phase.
Execution
The Steps of In the execution phase, the program is
Programming seen in a console (e.g., DOS Window) or as
an application.
Implementatio Maintenance
n A program or application sometimes
requires maintenance after it is placed into
production (in use). In this phase, sections
(modules) in the program may be deleted,
corrected or added to please the users’
needs.
20
21
Errors!
When writing and running code,
things happen!
A programming error is a fault
that occurs during program
compilation and or execution.
These errors either cause
programs to crash (stop) or
produce the incorrect result(s).
Syntax Errors - Errors in the use of the
language.
Every programming language has its own
rules, just like English or Spanish.
Syntax errors syntax errors are small
grammatical mistakes, sometimes limited to
a single character.
The compiler will complain about syntax
errors, the programmer must fix these errors
before the program can be compiled and ran.
Types of Errors Possible causes of syntax errors:
In Pascal, failure to end each line of code
with a semicolon will result in syntax
errors.
Misspelt variable names within the
program.
Missing keywords
No declaration of variables
22
Logic Errors - is a mistake in a
program's source code that results in
incorrect or unexpected behavior.
Logic errors occur when simple errors
are made by a programmer; the flow
or sequence of the program leads to
Types of Errors incorrect results.
Most logic errors occur in
mathematical operations where
incorrect symbols are used in
calculations.
23
Run - Time Errors
As the name suggests, runtime errors
occur while a program is being
executed. These errors are sometimes
cause by the input received from the
user, lack of memory space on the
computer or a popular calculation of
trying to divide a number by zero.
These errors are the most difficult
Types of Errors errors to trace because sometimes,
the exact cause may not be readily
visible.
A type of runtime error is a memory
leak. This type of error causes a
program to continually use up more
RAM while the program is running. A
memory leak may be due to an infinite
loop.
24
There are two ways in which a
programmer can locate errors within
his or her programs.
Finding and These are by using:
Fixing Errors Debugging Techniques
Testing Techniques
25
Debugging a program is a three-step
process:
Detection / Error Locating -finding
where the errors are located within
the source code
Debugging
Diagnosis - determine the cause of
Techniques the error
Correction
26
Several tools and techniques can be
used to carry out the debugging
process.
Debuggers – tools used to assist
programmers in locating errors in a
program.
Debugging Traces – Printout of the step-by-
step flow of the program.
Techniques
Variable Checks – Displays the
value of variables at different
points within the program.
Step Mode – The program is
executed line by line until the error
occurs.
27
28
Testing is the process of using
data within a program to
ascertain whether the correct
result is received.
Testing
Techniques This technique uses “test
data” or “test cases” which
are entered into the program
while it is being executed.
Test cases are documents that are used
to store information such as the input
data, expected results of the program,
Test Cases the actual results received after entering
the data and necessary comments.
29
“Dry Run” is the term used to describe
the checking of a program for errors
without the use of a computer or
compiler.
Dry run testing is a technique used by
Dry Run many programmers. This technique
involves the use of trace tables,
Testing algorithms and pseudo-codes. The
programmer “runs” through the
program, jotting the results down on
paper. This is done before any coding
starts (with a programming language).
30
31
The difference between testing and
debugging is that testing uses test
cases to find out if errors exist while
debugging tries to locate errors which
exist and work on correcting such
Debugging errors.
vs Testing
Often testing leads to runtime errors
which leads to debugging.
32
Overview of the Compiler Process
33
Developing Test Data Using the SBA
TRACETABLE
34
Programmin
g
PYTHON
Programming in Python 35
Python is a versatile and popular programming language known for its
simplicity and readability. It's widely used in various fields, including web
development, data science, machine learning, automation, and more.
How to install: To begin, you'll need to install Python on your computer. You
can download the latest version of Python from the official website
(https://www.python.org/downloads/) and follow the installation instructions
for your specific operating system.
https://www.youtube.com/watch?v=VkdkwxGka3M
An integrated development
environment (IDE) refers to a software
application that offers computer
programmers with extensive software
development abilities. IDEs most often
consist of a source code editor, build
automation tools, and a debugger.
Most modern IDEs have intelligent
Python IDE code completion. For Python we will
use PyCharm Community.
https://www.jetbrains.com/pycharm/do
wnload/?section=windows
Online IDE:
https://www.onlinegdb.com/online_pyt
hon_compiler
36
Documentation
Must have Developer Name, Program
Name, Description and Date.
Put this information in a comment section
at the beginning of the code.
#Keisha Morrison, Hello World, This
program displays “Hello World”, 6/9/2023
Initialize Variables
Python Syntax List all the veriables to be used with
Basics sample data to initialize it.
Add a comment above with the Data Type
#Strings
Name = “name”
Use comments to separate each section
of code and add labels.
#######While loop for accepting
data##########
37
38
Comments can be used to explain
Python code by humans for
humans.
Comments can be used to make
Python the code more readable.
Comments
Comments can be used to prevent
execution when testing code.
Comments starts with a #, and
Python will ignore them
#Begin
Begin is still the way to start your code.
A Begin comment will show the
developer where you begin your
program writing.
#End
Python Syntax Means it’s the end of your program all
Basics together. This is the last thing you type
Indentation
Indentation refers to the spaces at the
beginning of a code line.
Python uses indentation to indicate a
block of code. Useful for loops and if
statements.
39
40
Python Functions
Output Statement Input Statement
print() input()
Prints whatever in in the brackets Accepts user entered data.
with quotes.
Eg
EG
username = input("Enter the users
print(“Hello”); name:")
print(“Is your name ”, username,”?”)
Notice the comma to
separate the data.
Notice also the space.
41
Variable Names
There is no official declaration of variables in Python.
The variable is created with the first assignment. The variable comes into existence
when we assign a value to it for the first time. To initialize the variables, we can
assign temporary values right at the beginning of the program. To keep track of what
each variable is meant for, we can include comments that describe their purpose.
Variable Names are CASE sensitive. Name and name are seen as two different
variables!
All the rules of naming an Identifier applies. Can you list them?
Python allows you to assign values to multiple variables in one line:
x, y, z = "Orange", "Banana", "Cherry"
42
CREATE YOUR FIRST PROGRAM.
HELLO WORLD.
PROMPT THE USER FOR THEIR NAME.
Classwork ACCEPT THE USER'S NAME
YOUR PRGRAM SHOULD THEN SAY:
“HELLO WORLD, I MEAN <INSERT
USER’S NAME>”.
43
Manipulating User Input
All user input gets accepted as string/text. The following are different data types
Even if the user types the number 4 the Integer == Whole number int
program sees it as “4”. You cannot add “4” to Real (Float) == Decimals float
5. They are of different data types.
String == multiple letters or number str
So how do we get numbers from users? Type
casting!
Boolean == true or false == bool
E.g.:
If you want to specify the data type of a
int(“4”) converts the string “4” to the integer 4. variable, this can be done with casting.
float(“4”) converts the string “4” to the decimal
4.0
44
Assignment Statements Math Operators
The operator is replaced by =
Python follows BOMDAS!
E.g., num = 5
#the floor division // rounds the
result down to the nearest
whole number
Write three different source code files
to complete the following tasks:
a. Calculate the area of a circle,
given the radius.
b. Accept a student's name and
calculate the student’s average
grade given a homework, classwork
CLASSWORK: and a test grade. Print the grades as
a report.
c.Determine the price of an item
including an 8% tax. Output the
result as a receipt with one item.
Display the item name, initial cost,
tax and total cost inclusive of tax.
Research how to get the results in a
currency format. 45
46
Conditional IF STATEMENTS
Statements
47
Python
Logical
Comparisons
48
Simple If Statements
The THEN is replaced by a
colon
Pseudocode Python
BEGIN #BEGIN
a=3 a = 3
b=4 b = 4
if a > b:
IF (a>b) THEN
print(“a is greater than b")
PRINT “ a is greater than b”
#END
ENDIF
Even though there is no
ENDIF there is still the
indentation. Without the
indentation you get a
syntax error.
49
Multiple Conditions
You can test more than one condition within the same if statement.
a = 200
b = 33
if b > a:
print("b is greater than a") The else keyword is used
elif a == b: when all the above
print("a and b are equal") conditions fail.
else:
print("a is greater than b")
The elif keyword is used Note the indentation and the colons.
when the previous condition
is false, and you want test
another condition.
50
Nested Ifs
x = 41 It’s the indentation that lets you
know what statement(s) correspond
if x > 10: to the if condition.
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
51
52
Logical Operators
Logical operators are used to combine conditional statements:
Common letters. Python is
case sensitive.
CLASSWORK
The Jamaica Constabulary force is seeking to computerize their
recruitment procedures for the organization. You have been tasked with
creating a program that will check the applicant based on the 53
requirements for the organization. The program will accept the following
data for each applicant:
Their name
Their gender
Their height
Their weight
Their age
The program will state if the potential recruit meets the following
requirements and will keep doing so unless indicated to stop by the
user:
Males must be at least 160cm and weigh at least 135lbs
Females must be at least 140cm tall and weight at least 125lbs
All applicants must be between the ages of 18-30 years of age
20 Marks
Test 54
You are designing a simple grading system for a school. Each student's
final grade is determined based on their scores in three subjects: Math,
English, and Science. To pass, students must meet the following criteria:
The average score in all subjects must be 70 or higher.
No subject score should be below 50.
Write a Python program that takes input for a student's scores in Math,
English, and Science, calculates their average score, and then
determines whether the student has passed or failed.
Note: You can assume that all input scores are integers, and there's no
need to validate input in this exercise.
20marks
55
WHILE LOOP
Repetition FOR LOOP
56
While Lo0ps
While Loop Structure: i = 1
Initialize while i < 6:
print(i)
Loop i += 1
Stop Condition
accumulators must increment
inside loop
All statements “inside” the
loop must be indented on
With the break statement we can stop the same level.
the loop even if the while condition is
true
57
For Lo0ps
Pseudocode Python
FOR i = 0 to 4 DO for i in range(5): # Iterating through 0 to 4
PRINT i print(i)
The Range function - To loop through a set of
code a specified number of times, we can use the
range() function
The range() function returns a sequence of
numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a
specified number.
Note that range(6) is not the values of 0 to 6, but
the values 0 to 5.
58
Range Function
The range() function defaults to 0 as a starting value, however it is
possible to specify the starting value by adding a parameter: range(2,
6), which means values from 2 to 6 (but not including 6)
The range() function defaults to increment the sequence by 1, however
it is possible to specify the increment value by adding a third
parameter: range(2, 30, 3)
59
Classwork
Write a python program to read the names and ages of ten (10) people
and print the name of the oldest person.
There is no need to store all 10 names or ages. You only need to
store/print the oldest persons name and age.
60
Question Lo0ps
61
Classwork
Write a Python program to read ELEVEN numbers find their average and
print it. The algorithm should also print the number of times 6 occurs in
the data.
62
SBA Time
Let's take our skills and apply them to the SBA.
Develop python code that will accept patient information until the user enters 9999: PID, First
Name, Last Name, Gender, Total Hospital Cost and Covid Related Costs, Covid Status
(inconclusive, positive, or negative), Discharge Status, and Insurance type.
Calculate for each patient, what the insurance pays based on the percentages given in the
Database section and what the patient pays based on the criteria given in the Spreadsheet
section.
At the end you are to:
1. Display the number of positive patients who have either recovered or are in outpatient
care.
2. Display the number of persons who died and was positive for Covid.
3. Display the death rate i.e., percentage persons who have died from Covid..
Formatting is important.