Section5 ProgrammingConcepts
Section5 ProgrammingConcepts
Section5 ProgrammingConcepts
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
Programming languages
2
2022/04/27
3
2022/04/27
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
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
6
2022/04/27
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
7
2022/04/27
Control Structures
Sequence
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
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
11