Lesson 1 - Data Types and Operations
Lesson 1 - Data Types and Operations
efin e
in g ? D h
a m m r e a c
p ro g r p le fo
e d i n e xa m
o k : e s u s id e a n
te b o t a t y p p ro v
N o d a n d
Class t he m a in
t a t y p e s a
at a re in da
Wh f the ma
c h o
ea
o ne .
AQA
Computer Science –
GCSE (9 - 1)
Topic 2A:
Programming Basics
Lesson 2.01:
Data types and operations
We Are Learning To: Understand the importance
of pseudocode and their uses in programming.
SOLO What I’m Looking For:
Key Words:
Describe and use constants and variables when writing Variable
pseudocode and program code as well as common
arithmetic operations. Identifier
Data types
Analyse and use relational operators and interpret
them when used within algorithms Operations
Casting
Functions
Challenge: Each task has a challenge activity, in order to
evaluate, reflect, secure and extend your knowledge.
Variables and Data Types:
• Any data that a program uses must be stored in memory locations, each given tis own
identifier while the program is running.
• As the program runs, the values in these locations might change, which is why they are
called variables.
• For example, a variable called total might change several times as many numbers are
added to it.
• A variable called surname might change as a program processes a list of customers
orders.
• Each variable has an identifier (a unique name) that refers to a location in memory
where the data item will be stored.
• Each variable also has a data type that defines the type of data that will be stored at
the memory location and therefore the operation that can be performed on it (for
example, you can multiply two numbers but you can’t multiply two words)
• Remember that it is important for the readability of the program to use meaningful
identifier names, for example total rather than t, surname rather than s.
Declaring variables:
• In some programming languages variables are declared at the start of the program so that
when the program runs, the appropriate amount of memory can be reserved to store the data.
• The following are examples from Visual Basic programming:
• In any high-level language where you declare variables, the statement will include an identifier
and a data type.
• In some languages, for example Python, variables are not declared at all.
• The program assumes the data type of a variable based on what is put in it.
• So if you write a = 23, b = “Fred”, a will be stored as integer (whole number) and b will be
stored as a string (text)
• Even if you do not have to declare variables in your particular programming language, you need
to understand the different data types and how to work with them in the programs you write.
Declaring variables:
• The table below shows a list of some data types and the typical amount of memory that
each needs.
Data Type Type of Data Typical amount of memory
Integer A whole number, such as 3, 45, -453 2 or 4 bytes
Real/Float A number with a fractional part such as 34.456, -9.2, 4.10 4 or 8 bytes
Char/Character A single character, where a character can be any letter, 1 byte
digit, punctuation mark or symbol that can be types
String Zero or more characters. A string can be null (empty), just 1 byte per character in the string
one character or many characters
Boolean A Boolean variable has the value True or False 1 byte
• The programming language that you are using may have different names for some of these,
for example, “real” is called “float” in some languages.
• Your language will also have data types not listed.
• The amount of memory used for each data type varies for different programming languages
too – for example – integer may need two bytes in one language and four in another
Assignment statements:
• In most programming languages, values are
assigned to variables using an = sign.
• For Example:
• In pseudocode, we will use the ← symbol to mean
assignment, e.g. x ← 1
Constants:
• In some programs, there are certain values that remain the same (constant) while the
program runs.
• The programmer could use the actual value in the code each time, but it is good practice
to give the value a unique name (identifier) and then use that name throughout the
program.
• Constants are declared at the start of a program and can then be referred to as needed
in the code.
Constants:
• By convention, constant names are written in uppercase with an underscore (_) to
separate each word.
• E.g. VAT_RATE.
• This is known as snake case.
• For example:
• At the start of the program: constant VAT_RATE ← 0.2
• Later in the program code: sellPrice ← netPrice * VAT_RATE + netPrice
• Note, though, this does not mean that the constant’s value will never change during
the lifetime of the system!
• For example, the VAT rate will stay the same (constant) while the program runs in a
shop each day but, after the next budge, it may change.
• The value allocated to that constant will then be edited in the program.
Constants:
• The two main benefits of declaring a constant are:
• When its value changes, you only have to edit it in one place rather than
looking for every place in the program where you used that value
• The code will be easier to read and understand because the constant’s
identifier will be used instead of a number. This makes your code easier to
debug and later on, maintain.
• In some languages such as Python, there is no special way to declare
constants at the start of the program.
• You just write an assignment statement such as:
VAT_RATE = 0.2
Input and Output Statements:
Example 1:
• Write pseudocode for a program that asks the user to input their name.
• It should accept the name John and display Hello John on the screen.
• To get a substring:
String Manipulation:
• It is assumed that a string starts at position (index) 0, not 1, so stringExp[0] contains
the first character of a string called stringExp, or ’M’ in the case of Mongoose.
• To find the position of a character in a string:
Question 2:
Write pseudocode statements to input a first name and surname as string, for example
“Thomas Edison” and then use string manipulation functions to convert this to an initial and
surname and output, for example, “T Edison”
String Manipulation:
Concatenation:
• Concatenation means joining together two or more strings.
• Most languages uses the + operator.
Example:
• In the statement below, the result of each operation is shown in a comment on the
right.
• In the pseudocode that we will be using, all comments will start with the character #.
Question 3:
Write pseudocode to assign a 7-digit
number to an integer variable. Convert
this to a string, and output the middle
three digits
String Manipulation:
Converting characters to and from ASCII:
• ASCII stands for ‘American Standard Code for Information Interchange’.
• Using this code, every character can be represented as a 7-bit binary pattern,
allowing for 128 different characters to be represented.
• For example, ‘A’ is represented by 1000001, the same binary code as the decimal
number 65.
• ’B’ is ‘66’, ‘C’ is ‘67’, …. ‘Z’ is 90
Question 4:
Write pseudocode statements to accept an uppercase character from the user and output the
next character in the alphabet. If the user inputs Z, the letter A should be output.
We Are Learning To: Understand the importance of
algorithms and their uses in programming.
Task 1:
Key Words:
Complete the following Variable
questions linking to Identifier
pseudocode and Data types
programming on the Operations
worksheet provided. Casting
Functions