Programming (3)
Programming (3)
● A variable in a computer program is a named data store than contains a value that
may change during the execution of a program.
● A constant in a computer program is a named data store than contains a value that
does not change during the execution of a program.
Data Types
Input form the Keyboard and Output to the Display
Input and Output form the Keyboard
Arithmetic Operators
Selection
● Selecting a block of code to execute Start
No
End
Selection
● If and else Start
No
Output “You
are a Child”
End
Selection Start
● If else if
Input Mark1, Mark2, Mark3
No
Mark2>Mark1
Yes
and Output
mark2>mark3 ? Mark2
No
No
End
Selection Start
No
Mark2>Mark1
Yes
and Output
mark2>mark3 ? Mark2
No
No
End
Selection
● Nested If
Iteration(loops)
● Type of iterations
○ Count-controlled loops (for a set number of iterations)
○ Pre-condition loops – may have no iterations
○ Post-condition loops – always has at least one iteration.
Count-controlled loops
● Used when the number of iterations are known.
# iterate from i = 0 to i = 3
for i in range(4):
print(i) language = 'Python'
number = 1
All programming languages make use of logical operators to decide which path
to take through a program.
Logical operators
ClassAverage=0.0
StudentAverage=0.0
SubjectAverage=0.0
Student=0
Subject=0
Test=0
Name=””
Class=””
GENDER=False
String Handling
● Strings are used to store text. Every string contains a number of characters,
from an empty string, which has no characters stored, to a maximum number
specified by the programming language.
● The characters in a string can be labelled by position number. The first
character in a string can be in position zero or position one, depending on the
language.
String Handling
Creating a maintainable program
» always use meaningful identifier names for:
– variables
– constants
– arrays
– procedures
– functions
» be divided into modules for each task using:
– procedures
– functions
» be fully commented using your programming language’s commenting feature.
Use of nested statements(Cambridge)
NEXT Test
// Declarations of the variables needed SubjectAverage ← SubjectTotal / NumberOfTests
DECLARE ClassAverage, StudentAverage, SubjectAverage : REAL OUTPUT "Average mark for Subject ", Subject, " is ",
DECLARE Student, Subject, Test : INTEGER SubjectAverage
DECLARE ClassHigh, ClassLow, ClassTotal : INTEGER OUTPUT "Highest mark for Subject ", Subject, " is ", SubjectHigh
DECLARE StudentHigh, StudentLow, StudentTotal : INTEGER OUTPUT "Lowest mark for Subject ", Subject, " is ", SubjectLow
DECLARE SubjectHigh, SubjectLow, SubjectTotal : INTEGER IF SubjectHigh > StudentHigh
ClassHigh ← 0 THEN
ClassLow ← 100 StudentHigh ← SubjectHigh
ClassTotal ← 0 ENDIF
// Use of constants enables you to easily change the values for testing IF SubjectLow < StudentLow
CONSTANT NumberOfTests = 5 THEN
CONSTANT NumberOfSubjects = 6 StudentLow ← SubjectLow
CONSTANT ClassSize = 20 ENDIF
FOR Student ← 1 TO ClassSize StudentTotal ← StudentTotal + SubjectTotal
StudentHigh ← 0 NEXT Subject
StudentLow ← 100 StudentAverage ← StudentTotal / (NumberOfTests * NumberOfSubjects
StudentTotal ← 0 OUTPUT "Average mark for Student ", Student, " is ", StudentAverage
FOR Subject ← 1 TO NumberOfSubjects OUTPUT "Highest mark for Student ", Student, " is ", StudentHigh
SubjectHigh ← 0 OUTPUT "Lowest mark for Student ", Student, " is ", StudentLow
SubjectLow ← 100 IF StudentHigh > ClassHigh
SubjectTotal ← 0 THEN
FOR Test ← 1 TO NumberOfTests ClassHigh = StudentHigh
OUTPUT "Please enter mark " ENDIF
INPUT Mark IF StudentLow < ClassLow
IF Mark > SubjectHigh THEN
THEN ClassLow ← StudentLow
SubjectHigh ← Mark ENDIF
ENDIF ClassTotal ← ClassTotal + StudentTotal
IF Mark < SubjectLow NEXT Student
THEN ClassAverage ← ClassTotal / (NumberOfTests * NumberOfSubjects * ClassSize)
SubjectLow ← Mark OUTPUT "Average mark for Class is ", ClassAverage
ENDIF OUTPUT "Highest mark for Class is ", ClassHigh
SubjectTotal ← SubjectTotal + Mark OUTPUT "Lowest mark for Class is ", ClassLow
NEXT Test
Use of nested statements(edexcel)
NEXT Test
// Declarations of the variables needed SET SubjectAverage TO SubjectTotal / NumberOfTests
DECLARE ClassAverage, StudentAverage, SubjectAverage : REAL SEND "Average mark for Subject ", Subject, " is ", SubjectAverage TO DISPLAY
DECLARE Student, Subject, Test : INTEGER SEND "Highest mark for Subject ", Subject, " is ", SubjectHigh TO DISPLAY
DECLARE ClassHigh, ClassLow, ClassTotal : INTEGER SEND "Lowest mark for Subject ", Subject, " is ", SubjectLow TO DISPLAY
DECLARE StudentHigh, StudentLow, StudentTotal : INTEGER IF SubjectHigh > StudentHigh
DECLARE SubjectHigh, SubjectLow, SubjectTotal : INTEGER THEN
SET ClassHigh TO 0 SET StudentHigh TO SubjectHigh
SET ClassLow TO 100 ENDIF
SET ClassTotal TO 0 IF SubjectLow < StudentLow
// Use of constants enables you to easily change the values for testing THEN
SET CONSTANT NumberOfTests TO 5 SET StudentLow TO SubjectLow
SET CONSTANT NumberOfSubjects TO 6 ENDIF
SET CONSTANT ClassSize TO 20 SET StudentTotal TO StudentTotal + SubjectTotal
FOR Student 1 TO ClassSize NEXT Subject
SET StudentHigh TO 0 SET StudentAverage TO StudentTotal / (NumberOfTests * NumberOfSubjects
SET StudentLow TO 100 SEND "Average mark for Student ", Student, " is ", StudentAverage TO DISPLAY
SET StudentTotal TO 0 SEND "Highest mark for Student ", Student, " is ", StudentHigh TO DISPLAY
FOR Subject 1 TO NumberOfSubjects SEND "Lowest mark for Student ", Student, " is ", StudentLow TO DISPLAY
SET SubjectHigh TO 0 IF StudentHigh > ClassHigh
SET SubjectLow TO 100 THEN
SET SubjectTotal TO 0 SERT ClassHigh TO StudentHigh
FOR Test 1 TO NumberOfTests ENDIF
SEND "Please enter mark " TO DISPLAY IF StudentLow < ClassLow
RECEIVE Mark FROM KEYBOARD THEN
IF Mark > SubjectHigh SET ClassLow TO StudentLow
THEN ENDIF
SET SubjectHigh TO Mark SET ClassTotal TO ClassTotal + StudentTotal
ENDIF NEXT Student
IF Mark < SubjectLow SET ClassAverage TO ClassTotal / (NumberOfTests * NumberOfSubjects * ClassSize)
THEN SEND "Average mark for Class is ", ClassAverage TO DISPLAY
SET SubjectLow TO Mark SEND "Highest mark for Class is ", ClassHigh TO DISPLAY
ENDIF SEND "Lowest mark for Class is ", ClassLow TO DISPLAY
SET SubjectTotal TO SubjectTotal + Mark
NEXT Test
Arrays
● An array is a data structure containing several elements of the same data type.
● these elements can be accessed using the same identifier name.
● The position of each element in an array is identified using the array’s index.
One-dimensional arrays
● A one-dimensional array can be referred to as a list. Here is an example of a list with 10
elements in it where the first element has an index of zero.
● When a one-dimensional array is declared in pseudocode:
■ the name of the array
■ the first index value
■ the last index value
■ and the data type
○ DECLARE MyList : ARRAY[0:9] OF INTEGER
■ we can add the number 27 to the fourth position in the array MyList as follows:
○ MyList[4] ← 27 or MyList[4] = 27
■ display the data that lies in a particular location in an array
○ OUTPUT MyList[1]
OUTPUT "Enter these 10 values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 72"
FOR Counter ← 0 TO 9
OUTPUT "Enter next value " SEND "Enter these 10 values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 72" TO DISPLAY
INPUT MyList[Counter] FOR Counter is 0 TO 9
NEXT Counter SEND "Enter next value " TO DISPLAY
RECEIVE MyList[Counter] FROM KEYBOARD
NEXT Counter
Two-dimensional arrays
● A two-dimensional array can be referred to as a table, with rows and columns.
● Here is an example of a table with 10 rows and 3 columns, which contains 30 elements.
● The first element is located at position 0,0.
● When a two-dimensional array is declared in pseudocode:
» the first index value for rows
» the last index value for rows
» the first index value for columns
» the last index value for columns
» and the data type
● DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER
Two-dimensional arrays
● this time there need to be two nested loops
OUTPUT "Enter these values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 34"
OUTPUT "Enter these values in order 31, 67, 98, 22, 35, 46, 71, 23, 11, 76"
OUTPUT "Enter these values in order 17, 48, 29, 95, 61, 47, 28, 13, 77, 21"
FOR ColumnCounter ← 0 TO 2
FOR RowCounter ← 0 TO 9
OUTPUT "Enter next value "
INPUT MyTable[RowCounter, ColumnCounter]
NEXT RowCounter
NEXT ColumnCounter
SEND "Enter these values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 34" TO DISPLAY
SEND "Enter these values in order 31, 67, 98, 22, 35, 46, 71, 23, 11, 76" TO DISPLAY
SEND "Enter these values in order 17, 48, 29, 95, 61, 47, 28, 13, 77, 21" TO DISPLAY
FOR ColumnCounter FROM 0 TO 2
FOR RowCounter FROM 0 TO 9
SEND "Enter next value " TO DISPLAY
RETRIEVE MyTable[RowCounter, ColumnCounter] FROM KEYBOARD
NEXT RowCounter
NEXT ColumnCounter