2022/04/27
Programming
Concepts
Section 5
Programming concepts
Outline
Introduction
Programming languages
Program translators
Structured programming techniques
Introduction to algorithms
Program flowcharts
Pseudo code
Control Structures
Sequence of instructions
Selection
IF….ELSE
Nested IF Statement
Repetition/Iteration
FOR, WHILE, REPEAT…until
Check algorithm logic using trace table
1
2022/04/27
Introduction: Defn of Terms Programming concepts
Computer programming is the process of writing, testing,
debugging/troubleshooting, and maintaining the source
code of computer programs.
This source code is written in a programming language like
C++, JAVA, Perl etc
A program is a set of instructions following the rules of
the chosen language.
A programmer is someone who writes computer
program.
Computer programmers write, test, and maintain programs
or software that tell the computer what to do
A programming language is a software used by
programmers to develop programs or software
Programming languages can be used to create computer
programs.
Programming languages
Generations of Programming Languages
The first generation languages, or 1GL, are low level
languages that are machine language.
The 2nd generation languages, or 2GL, are also low-level
languages that generally consist of assembly languages.
The 3rd generation languages, or 3GL, are high level
languages such as C, JAVA, COBOL, FORTRAN etc
The 4th generation languages, or 4GL, are languages that
consist of statements similar to statements in a human
language. Commonly used in database programming and
scripts, e.g. SQL
The 5th generation languages, or 5GL, are programming
languages that contain visual tools to help develop a
program. A good example of a fifth generation language is
Visual Basic
The first two generations are called low level languages
and the next three are called high level languages
2
2022/04/27
Low level languages
Machine language
Machine language is the only language that uses
binary digits (0s and 1s), to represent instructions.
The only language understood by computers.
For example: 10110000 01100001
Machine code instructions do not need a
translation program (compiler and/or interpreter) to
run, because they are already in the language the
computer understands.
hence such programs are faster to execute.
While easily understood by computers, machine
languages are almost impossible for humans to use
because they consist entirely of numbers
Low level languages
Assembly language
A program written in assembly language consists of a
series of instructions mnemonics that correspond to a
stream of executable instructions that can be loaded
into memory and executed.
Assembly languages use keywords and symbols, much like
English, to form a programming language (e.g. SUB, MUL
and DIV).
These are easier for humans to remember and use, as
compared to machine code.
For example: mov a1, #061h
Meaning: Move the hexadecimal value 61 (97 decimal)
into the processor register named "a1".
Assembly languages are called low level languages
because they are very similar to machine code in
concepts and structure.
They refer to the computer hardware directly
An assembler translates the mnemonics of assembly
language programs into machine language instructions
for the microprocessor to execute.
3
2022/04/27
High level languages
High-level languages are written in a form that is close to
our human language (i.e.) English like words- for
example words like: print, if, while are used).
Examples: JAVA, C, C++, COBOL, Python etc
Programs written in a high-level language need to be
translated into machine language before they can be
executed.
Some programming languages use a compiler to perform
this translation and others use an interpreter
Hence slower to execute.
Advantages:
Easier to code and modify as it is uses English like
statements
Easier to debug as it is uses English like statements
Portability- not designed to run on just one type of machine
Programming languages
Low vs High level languages
High level languages Low level languages
Can be run on a number of Machine dependant
different types of computer with
only minor changes
Uses more memory during Uses less memory during translation
translation
It is easy to write and maintain It is difficult to write and maintain
programs programs
Easier to debug Difficult to debug
High level language is closer to Closer to the machine code
the human (spoken language)
Programs executes slower Programs executes faster
4
2022/04/27
Program translators
Assembler, Complier, Interpreter
Program translators are used to convert programs
written in high level languages or low level languages
(source code) to machine code (object code).
Programs written in assembly language or high-level
languages need to be translated into machine code
before they can be executed by a computer.
The instructions of a high-level language are called the
source code. The translated program to machine code
is the object code.
There are three types of program translator: assemblers,
compilers and interpreters.
Assembler: are used to translate a program written in
assembly language into a machine code, so it can be used
and executed by the computer.
Interpreter: are able to read, translate and execute one
statement at a time from a high-level language program to
machine code.
Compilers: are used to translate the entire program written
in a high-level language into machine code.
Program translators
Assembler vs complier vs interpreter
Compiler Interpreter Assembler
Translates the entire high- Temporarily executes high- Translates low-level assembly
level languages into machine level languages, one code into machine code
code statement at a time
Object Code is saved No Object Code is Generated Object Code is saved
Compiled programs no Interpreted programs cannot Assembled programs no
longer need the compiler be used without the longer need the assembler
interpreter
Error report produced once Error message produced
entire program is immediately (and
compiled. Difficult to debug program stops at that point)
Easier to debug
Compiling may be slow, but Interpreted code is run
the resulting program code through the interpreter , so it
will run quick (directly on the may be slow, e.g. to execute
processor) program loops
One high-level language
statement may be several
lines of machine code
when compiled
5
2022/04/27
Structured programming
Introduction
A methodical approach to design of a program
which emphasizes breaking large and complex
tasks into smaller sections, known as modules, sub-
routines, subprograms and procedures.
Techniques used in structured programming:
Top- down: the problem is defined and then split into
a number of smaller sections in order to comprehend
each section.
Bottom-up: programming a number of small modules
and building them into a large structure
Stepwise refinement: the 1st step is top down
approach, followed by decomposition of subparts into
more detailed instructions. In each of the next steps
one more instructions are decomposed into more
detailed ones. This refinement process terminates
when one reaches what has been defined as
elementary process.
Structured programming
Advantages
Allows easy debugging
For easy writing of computer programs
They allow large program to be written by several
people which saves time.
Progress on a large project can be easily
measured.
Modules can be tested individually.
Complex programs can be made easy by splitting it
to smaller sections.
6
2022/04/27
Algorithm tools in solving problems
Introduction
A sequence of instructions.
A procedure or formula for solving a problem.
It was created mathematician, Mohammed ibn-Musa
al-Khwarizmi.
Often used for calculation, data processing and
programming.
Algorithms can be expressed in any language. In
Computer Science, Algorithms can be written as
pseudocode or flowcharts
Algorithms for making things will often be divided into
sections; – the parts/components/ingredients (inputs)
required to accomplish the task – actions/steps/methods
(processing) to produce the required outcome (output).
For example to build a model car, the parts (inputs) are
needed plus instructions on how to assemble the car
(processing) and the result is the car (output).
Algorithm2: Step1: Read\input the Radius r of the Circle
Step2: Area=PI*r*r // calculation of area
Step3: Print Area
Algorithm tools in solving problems
Flowchart
Flowchart is a graphical way of representing an algorithm
design.
Each symbol represents a different type of operation.
Common symbols are:
7
2022/04/27
Algorithm tools in solving problems
Pseudocode
Pseudo-code (which means fake code, because its
not really programming code) specifies the steps
required to accomplish the task
A type of structured English that is used to specify
an algorithm
Cannot be compiled nor executed, and there are
no real formatting or syntax rules
Once a problem has been solved logically using
pseudo-code, it can then be coded into an actual
programming language of choice.
Common words used in pseudo-codes are:
begin…end: used to start and finish a program.
Input/read (accept): obtain input from a user.
Display/ouput: used to present the result or an output.
If…else, while, etc: used in decision making.
Control Structures
Sequence
A series of actions are performed in sequence
the one above or before another gets executed first
Example 1: to calculate pay Start
BEGIN
Input hours Hours,
Input rate rate
pay = hours * rate
Display pay
pay=hours
END *rate
Example 2: Convert F to C
BEGIN pay
Read Temperature in Fahrenheit F
C=5/9*(F32)
Stop
Print Temperature in Celsius: C
END
8
2022/04/27
Control Structures
Selection/Branching
Once the condition is evaluated, the control flows into one of two paths
This is usually represented by the ‘if-then’ construct in pseudo-codes
In flowcharts, this is represented by the decision symbol
IF statement…Syntax Start
IF Condition THEN
Value IF TRUE
grade
ELSE
Value IF FALSE
ENDIF
Is
Yes gra No
e.g. : Check student marks de>
BEGIN 50?
Input grade
IF(grade>50) THEN PASS FAIL
Output (PASS)
ELSE
Output (FAIL)
ENDIF
END Stop
Control Structures
Selection-Nested IF Statement
BEGIN
IF (logical expression_1)THEN Input total_grade
statement_1; IF (total_grade >= 80) THEN
ELSEIF(logical expression_2) THEN letter_grade = ‘A’;
statement_2;
ELSEIF(total_grade>= 70)THEN
.
.
letter_grade = ‘B’;
ELSEIF(logical expression_n)THEN ELSEIF(total_grade>= 60)THEN
statement_n; letter_grade = ‘C’;
ELSE ELSEIF(total_grade>= 50)THEN
statement_n+1;
letter_grade = ‘D’;
ELSE
letter_grade = ‘F’;
ENDIF
PRINT (letter_grade)
END
9
2022/04/27
Control Structures
Selection/Branching
Draw a flowchart to find the largest of three numbers A, B,
and C
Dry run the algorithm using A=17, B=15, C=20
Control Structures
Repetition/Iteration
The loop allows a statement or a sequence of
statements to be repeatedly executed based on some
loop condition
It is represented by the ‘while’, ‘for’ and ‘repeat…until
in most programming languages
WHILE condition DO REPEAT FOR counter= x to n DO
statements statements statements
ENDWHILE UNTIL condition is TRUE ENDFOR
• Repeat-Until Condition is checked at the end of the loop
• While-Do Condition is checked at the very beginning
• For number-of-times Do Loop ‘runs’ a specific amount of time
Examples: Begin Begin
Sum of First sum=0, n=0 sum=0, n=0 Begin
50 natural WHILE n<=50 DO REPEAT sum=0
numbers n=n+1 n=n+1 FOR n=0 to 50 DO
sum=sum + n sum=sum + n sum=sum + n
ENDWHILE UNTIL (n=50) ENDFOR
Print (sum) Print (sum) Print (sum)
End End End
10
2022/04/27
Control Structures
Repetition/Iteration
NO
algorithms
Use of procedures/subroutines
Sub-routines/procedures are sequence of
programs instructions that performs a specific task,
but available for repeated use. Examples are
functions
Benefits of subroutines are:
Foreasy debugging
Foreasy writing of computer programs
Makes programs shorter
Quicker to derive to a solution
Importance of program annotations
They make the program easy to understand (i.e.)
program notes
11