Professional Documents
Culture Documents
We recommend the OCR endorsed text book from PG Online for use during your GCSE studies.
Craig'n'Dave videos for SLR 2.2
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
Variable: A named location in memory whose value can change. Constant: A named location in memory whose value cannot change.
Example annotated program: Python Example annotated program: OCR reference language
#Program to calculate the VAT of an ex-VAT item. //Program to calculate the VAT of an ex-VAT item.
cost = input("Enter the ex-VAT price of the item: £") cost = input("Enter the ex-VAT price of the item: £")
The use of the three basic programming constructs: sequence, selection and iteration
The use of the three basic programming constructs: sequence, selection and iteration
switch username.length:
case < 4:
print("Too few characters.")
do
case > 12:
username = input("Enter a chosen username: ")
print("Too many characters.")
until username.length < 4 or username.length > 12
default:
print("Valid username chosen.")
endswitch
Not supported by all languages, this construct is an alternative to using Not supported by all languages, this iteration is different because it will
else if or elif commands. execute the code inside the loop at least once.
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
The output for 6 DIV 2: 3 The output for 6 MOD 2: 0 The output for 6 * 2: 12
The output for 10 DIV 3: 3 The output for 10 MOD 3: 1 The output for 10 / 3: 3.33…
The output for 8 DIV 7: 1 The output for 8 MOD 7: 1 The output for 10 / 3: 3.33…
The output for 13 DIV 5: 2 The output for 13 MOD 5: 3 The output for 13 / 5: 2.6
The output for 2 DIV 4: 0 The output for 2 MOD 4: 2 The output for 2 ^ 4: 16
Program to output ----- every 25 iterations: Function to return whether an input number is odd or even:
Data types
Data Types
Data type Description Example assignment
Casting operations
Example annotated program: Python Example annotated program: OCR reference language
#Program to calculate the VAT of an ex-VAT item. //Program to calculate the VAT of an ex-VAT item.
cost = input("Enter the ex-VAT price of the item: £") cost = input("Enter the ex-VAT price of the item: £")
if user == "":
end_of_file = True
user = text_file.readline().strip()
text_file.close()
end_of_file = True
end_of_file = False
if user == username:
login_date = text_file.readline().strip()
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
Data can be written, overwriting all the username = input("Enter your username:")
existing data in a file.
text_file.write(username+"\n")
text_file.write(date+"\n")
text_file.close()
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
Use of records
Most languages let you define your own data structures in the form of records.
A complete example:
procedure main()
student1 As TStudent
student1.firstName = "Jeff"
student1.surname = "Williams"
student1.depositPaid = 36.0
endprocedure
We can’t use a single array data structure to store this data because:
Many programming languages allow you to store your record data in a database such as Access, MySQL etc. and access it in the program using SQL.
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
The use of records to store data & SQL to search for data
SQL commands: SELECT – which fields (* is a wildcard), FROM – which table, WHERE – criteria (LIKE used with % as a wildcard)
Assuming a database table called ‘world’ has a record structure of: country, continent, area, population, gdp, capital:
SQL to output the population of Germany: SELECT population FROM world WHERE country=‘Germany’
SQL to output the name of all countries SELECT country FROM world WHERE population > (SELECT population FROM world WHERE
with a population greater than Russia using country='Russia')
a nested SELECT statement.
Arrays
bingo_ticket
index 0 1 2 3 4 5 6 7 8
The array is zero-indexed. Assuming the indexes refer to the column followed by the row:
x = bingo_ticket[3,1] “34”
x = bingo_ticket[7,0] “75”
x = bingo_ticket[5,2] “59”
Setting the value in the index identified with a ? to 28: bingo_ticket[2,1] = “28”
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
Arrays
Two dimensional arrays can also be used to emulate database tables, providing all data is stored as a string.
It doesn’t matter whether you visualise the indexes being [row, column] or [column, row] providing you are consistent throughout the program.
Therefore, if world_database[2,1] = “Africa”, then:
world_database[2,5] is: “Algiers” To change the GDP of Algeria to be 300000 would require the statement:
world_database[2,0] = “300000”
world_database[3,2] is: “468”
#Main program
15: import random
16: print("This tool outputs the value of two dice")
17: output_throw()
The purpose of a procedure:
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
RainDay("Tuesday")
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
The ability to generate a random number in programming can be very useful. There are many situations when you might want to use this especially in
games programming, some examples are:
Assume the following line of pseudo-code produces a random whole number between 1 and 6:
x 🡸 random(1, 6)
Write the pseudocode for a program which will throw a 6-sided die 100 times.
An array called roll[1..6] store the total number of times each number is rolled.
The program should end by outputting how many times each number was rolled.
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
Feedback
Breadth Depth Understanding
□ Some aspects complete □ Basic level of depth shown □ Some work is accurate
□ Little work complete □ Little depth and detail provided □ Little work is accurate