8 - Programming Concepts_2023
8 - Programming Concepts_2023
Algorithm
An algorithm describes a set of steps to solve a
problem
8. Programming concepts • We can use pseudocode to describe in words how algorithms work
• We can use flowcharts to graphically display an algorithm
Mr Hassine Mr Hassine
1
1 2
3 4
Pseudocode
Pseudocode some from the Greek work pseudo which means ‘fake’ –
meaning that pseudocode is not a programming language
5 6
1
13/05/2023
Data Types
Mr Hassine
7 8
Variables
Stores a value that can change during the execution of the
program Can use a variable without knowing its value
age 19
Assigning values
height 1.8
to variables
Mr Hassine
gender “m” Mr Hassine
9 10
9 10
Constant
• A constant is a value that cannot be changed accidentally during
the execution of the program
• Value only needs to be changed once if circumstances
change/during the initialisation process
What is a constant?
CONSTANT pi 3.14
Mr Hassine Mr Hassine
11 12
11 12
2
13/05/2023
Declaring variables
DECLARE age: INTEGER
DECLARE height: REAL
DECLARE gender: CHAR
DECLARE name: STRING
DECLARE paid: BOOLEAN
DECLARE ARRAY studentNames[1:50] OF STRING
What is an array?
DECLARE ARRAY
DECLARE ARRAY grades [ 1:50 , 1:3 ] OF CHAR
50 rows 3 columns
Mr Hassine Mr Hassine
13 14
Array A constant is a type of variable that does not change throughout the
Arrays allow us to: life time of the program
• Store list of items of the same data type stored under a single name
• To reduce the number of variables used CONSTANT pi 3.14
• Any item can be found using an index number to show its place in the
list cost ← 16
price ← Cost * 2
tax ← Price * 0.25
DECLARE ARRAY names[1:5] OF STRING
sellingPrice ← Price + Tax
gender ← “m”
Names [“Sara”, “Ben”, “Lee”, “Khalid”, “Lina”] chosen ← False
total total + number
Mr Hassine
count count + 1 Mr Hassine
15
15 16
2D Array
2D arrays can be thought of as a table with rows and columns
17 18
3
13/05/2023
Procedure
A Procedure is a subroutines that carry out a but does not
supply any output to other routines
pay(15, 10)
Mr Hassine Mr Hassine
20
19 20
Functions 2 Parameters
Functions are routines that create an output that can be used
by other routines
FUNCTION pay (rate: REAL, hours: REAL) RETURNS REAL
print( ) input( ) max( ) print( ) wage hours * rate
RETURN wage
round( ) str( ) int( ) ENDFUNCTION
min( ) Function is being called.
Procedures Ben pay(15, 10) We are passing arguments
Sayed pay(1.5, 80) into the function when it is
Procedures or sub-procedures are routines that carry out tasks called
but do not supply any output to other routines Functions must always have
Mr Hassine
return values Mr Hassine
21 22
Selection
Selection allows the user to find a different route
through a program
Conditional statement
IF…THEN…ELSE…ENDIF
CASE…OF…OTHERWISE…ENDCASE
Mr Hassine
23 24
4
13/05/2023
25 26
CASE…OF…OTHERWISE…ENDCASE
CASE can be used to decide what happens
depending on the value
DECLARE grade: CHAR
LOOPS (iteration)
INPUT grade
CASE grade OF
“A”: PRINT “Outstanding”
“B”: PRINT “Good”
“C”: PRINT “Average”
OTHERWISE PRINT “Needs improvement”
ENDCASE
Mr Hassine
27 28
ITERATION FOR…TO…NEXT
• Some actions need to be repeated in an algorithm
FOR counter ← 1 TO 10
• There are three types of loop structures PRINT “Enter student name”
INPUT
Operator Comparison StudentName[counter]
NEXT
Set number of repetitions FOR…TO…NEXT
PRINT “Ten students added”
Repetition, where number of loops
is not known – must be completed at REPEAT…UNTIL
least once
• FOR Loop must have start an stop value
Repetition, where the number of • The FOR loop counts up until the stop value
repeats is not known, may never be WHILE…DO…ENDWHILE • Must indent the lines up until the stop value
completed
29 30
5
13/05/2023
WHILE…DO…ENDWHILE REPEAT…UNTIL
• No need to set up test variables before loop starts
PRINT “You are stuck in a maze” • Commands inside the loop are carried out at least once before the test
INPUT direction direction ← “n”
WHILE ( direction <> “W” ) DO REPEAT
PRINT “You are stuck in a maze”
PRINT “You are in a maze” READ direction
INPUT direction UNTIL direction = “w”
PRINT “You have escaped”
ENDWHILE
PRINT “You have escaped”
• Have to set the value of the test variable before the loop starts
• If test result is FALSE the commands inside the loop will not be
carried out at all
31 32
Mr Hassine Mr Hassine
33 34
33 34
Mr Hassine Mr Hassine
35 36
35 36
6
13/05/2023
Mr Hassine Mr Hassine
37 38
37 38
Purpose of subroutine in a program • Sub-program / system not the whole program / system
• To perform a frequently used operation within a program
• That can be called when needed
• That can be reused by another program
Mr Hassine Mr Hassine
39 40
39 40
Mr Hassine
41
41