0% found this document useful (0 votes)
1 views

Lesson 1 - Data Types and Operations

Lesson 1- Data Types and Operations

Uploaded by

Kareem Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lesson 1 - Data Types and Operations

Lesson 1- Data Types and Operations

Uploaded by

Kareem Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Do Now Activity:

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:

Identify and explain each of the data types used in


programming.

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.

• The first statement on the screen:

• And waits for the user to enter something.


• If the user types John, the OUTPUT statement then displays on the screen:
Operations on data:
• Operations are things you can do to specific types of data.
• For example, you can perform arithmetic operations on numbers and you can
perform string-handling operations on text.
Numeric Data Types:
• Operations that can be performed on numerical data types are shown here:
Arithmetic Operations Comparison Operations
(Numerical Result) (Boolean Result: True or False)
Eg: 25 + 3 = 28 Eg: 456 > 34 is True
+ Addition < Less than
- Subtraction > Greater than
* Multiplication <= Less than or Equal to
/ Division >= Greater than or Equal to
DIV or // Integer Division != or <> Not equal to
MOD or % Modulus = or == Equal to
Operations on data:
• The arithmetic and Boolean operators vary slightly between different programming
languages.
• Where there are differences, the first option will be used in exam questions.
• When performing operations on data items you need to consider the data types used.
• For example, in a simple calculation where two whole numbers are added together,
variables could be defined as follows in C#
• Int num1, int num2, total;
• But if the calculation involves division, then the answer variable should be declared as a
real number because the result is unlikely to be a whole number:
• Int num1, num2;
• Float answer;
• The arithmetic operators DIV and MOD can only be performed on whole number (integers)
• DIV is integer division, it works like normal division but returns the whole number of times
one number goes into the other
Operations on data:
• Here is some examples:
13 DIV 3 = 4
30 DIV 3 = 10 Question 1:
32 DIV 3 = 10 State the values of w, x, y and z that
• MOD gives the remainder of integer will be output when these
division as follows: statements are executed:
13 MOD 3 = 1 W ← 54
30 MOD 3 = 0 X ← w MOD 7
32 MOD 3 = 2 Y ← x DIV 2
• The exponentiation operator calculates Z ← Y**3
the power of a number, so for example is OUTPUT w, x, y, z
the same as writing 7**2 in Python.
String handling operations:
• Sometimes you need to change the type of variable from a string to an integer or a
real number or vice versa.
• This is done with functions such as:

• Python, for example, accepts all user input as string, not


numbers.
• You may need to convert a string to a number so that you can
use it in a calculation.
• Consider the following program in Python:
• If the user enters the number 3 & 5, you would expect num3
to be 8. However the output is:
String handling operations:
• What has happened?
• The numbers are input as string, and the + symbol when used with string means
concatenation, or joining the two strings together.
• You need to convert each if the strings to integers or floating point numbers.
• A corrected program would look like this:

• In Python, the int function changes the variable type to an integer.


• This is called Casting
String Manipulation:
• A string is a sequence of characters enclosed in quote marks.
• The programming languages have built-in functions to manipulate strings.
• The functions given below are given in the pseudocode which will be used in an
exam.
• Each programming language will have its own particular functions and syntax
• To get the Length of a string:

• 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

Challenge Activity: Extension Activity:


Using the internet, review the Complete the definitions for
methods of writing Selection each of the key words listed
statement using Pseudocode. in the orange box.
We Are Learning To: Understand the importance of
Pseudocode and their uses in programming.

SOLO What I’m Looking For:

Identify and explain each of the data types used in


programming.

Describe and use constants and variables when


writing pseudocode and program code as well as
common arithmetic operations.
Analyse and use relational operators and interpret
them when used within algorithms

Challenge: Each task has a challenge activity, in order


to evaluate, reflect, secure and extend your
knowledge.

You might also like