2.2 Workbook 1
2.2 Workbook 1
Name: Zain
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 can not change
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.")
case > 12: do
print("Too many characters.") username = input("Enter a chosen username: ")
default: until username.length < 4 or username.length > 12
print("Valid username chosen.")
endswitch
Not supported by all languages, this construct is an alternative to Not supported by all languages, this iteration is different because it
using ELSE IF or ELIF commands. will 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: 1 The output for 13 / 5: 2.6
The output for 2 DIV 4: 0.5 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:
Number=int(input(“please enter
for counter in range(0,101):
number”
print (counter) If number% 2==0
if counter == 25:
print("-----") Print(“number is even”)
Else:
(“number is odd”)
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
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 also be appended (added to the username = input("Enter your username:")
end) of an existing file.
import datetime
Data cannot be inserted into the middle
of a file. text_file = open("users.txt", "a")
date = str(datetime.date.today())
text_file.write(date+"\n")
text_file.write(username+"\n")
text_file.close()
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
Using records
Most languages let you define your own data structures in the form of records.
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 and 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 contry=“Germany”
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 as row/column or column/row, providing you are consistent throughout the program.
Therefore, if world_database[2,1] = “Africa”, then:
To change the GDP of Algeria to 300000 would require the statement:
world_database[2,5] is: algiers
world_database[2,4] = “300000”
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
RainDay("Tuesday")
GCSE J277 Unit 2.2 | Programming fundamentals Craig’n’Dave
The ability to generate a random number can be very useful in programming. There are many situations when you might want to use this, especially in
video game programming. Here are some more examples:
Assume that the following line of pseudocode produces a random whole number from 1–6:
x random(1, 6)
Write the pseudocode for a program that throws a six-sided dice 100 times. An
array called roll[1..6] stores the total number of times each number is rolled. The
program should end by outputting how many times each number was rolled.
import random
count1=0
count2=0
count3=0
count4=0
count5=0
count6=0
dice=random.randint(1,7)
for i in range(10000):
if dice==1:
count1+=1
if dice==2:
count2+=1
if dice==3:
count3+=1
if dice==4:
count4+=1
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
I can explain how to input data to a program and write it out again.
I can explain the three basic programming constructs: sequence, selection, iteration (count- and condition-controlled).