13/05/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
4 Constructs in Computer Science
What are the 4 constructs in computer
science? Constructs
• Sequence – order in which the program in executed
• Assignment – assign a value to a variable
• Selection – select a route through a program
• Iteration – loop/repeat instruction
Mr Hassine Mr Hassine
3 4
3 4
Pseudocode
Pseudocode some from the Greek work pseudo which means ‘fake’ –
meaning that pseudocode is not a programming language
- CHAR NEXT STRING
What is pseudocode? *
/
DIV
ELSE
NOT
OR
THEN
TRUE
<> ENDCASE OTHERWISE UNTIL
+ ENDIF OUTPUT WHILE ... DO
← FOR ... TO PRINT STEP
AND IF READ MOD
ARRAY INPUT REAL CASE OF
BOOLEAN REPEAT INTEGER //
FUNCTION PROCEDURE RETURN WRITEFILE
Mr Hassine Mr Hassine
5 6
1
13/05/2023
Data Types
We can assign values to specific data types using the following:
Data types Data Type Meaning
INTEGER A whole number
REAL A number capable of containing a fractional part
CHAR A single character
STRING A sequence of zero or more characters
BOOLEAN The logical values TRUE and FALSE
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
DECLARE age: INTEGER
What is a variable? DECLARE height: REAL
DECLARE gender: CHAR
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
ARRAY Students [1 : 5 , 1 : 3 ] OF STRING
What is a 2D Array row number/number of rows column number/number of columns
[1] [2] [3]
[1] Salem Smith B Students [ 1 , 2 ] “Smith”
[2] Salah Touati A
[3] Nasser Al Ouni B
[4] Michael Jones B Students [ 5 , 3 ] “C”
[5] Arun Sharma C
Mr Hassine Mr Hassine
17 18
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
What is a procedure? PROCEDURE pay (rate: REAL, hours: REAL)
DECLARE wage: REAL
wage hours * rate
PRINT wage
ENDPROCEDURE
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
IF (mark > = 80)
IF…THEN…ELSE…ENDIF
IF…THEN…ELSE…ENDIF THEN PRINT “A*”
NESTED IF example
ENDIF
A condition can be either TRUE or FALSE ELSE IF (mark >= 70) A condition can be either TRUE or FALSE
THEN PRINT “B”
END IF
INPUT mark
ELSE IF (mark >= 60)
IF mark > =50 THEN PRINT “C”
THEN PRINT “Pass” END IF
ELSE ELSE IF (mark >= 50)
THEN PRINT “D”
PRINT “Fail”
END IF
ENDIF ELSE PRINT “Fail”
ENDIF
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
LENGTH( ) UPPER( ) - for upper case
The LENGTH( ) returns the length of the string The UPPER( ) returns the upper case in the parameter
name ‘Mr Hassine’ OUTPUT name ‘Mr Hassine’ OUTPUT
numOfChars LENGTH(name) 10 newName UCASE(name) MR HASSINE
PRINT numOfChars PRINT newName
Mr Hassine Mr Hassine
35 36
35 36
6
13/05/2023
LOWER( ) - for lower case substring( ) - returns part of a string
The LOWER( ) returns the lower case in the parameter The SUBSTRING( txt , start , num ) has three parameters
and returns part of a string
name “MR HASSINE” OUTPUT
newName LOWER(name) mr hassine name “Mr Hassine” OUTPUT
PRINT newName newName SUBSTRING(name,3,7) hassine
PRINT newName
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