0% found this document useful (0 votes)
34 views30 pages

T1 Data Types and Operations

Programming Concepts Unit 8 - Data Types and Operations

Uploaded by

Nazril Rosly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views30 pages

T1 Data Types and Operations

Programming Concepts Unit 8 - Data Types and Operations

Uploaded by

Nazril Rosly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Objectives

• Understand and use data types: integer, real, char,


string and Boolean
• Declare and use constants and variables
• Use input, output and assignment statements
• Use arithmetic operators including MOD and DIV
• Use string handling functions
Data types and operations
Unit 9 Key programming concepts

Starter
• What is a data type?
• How many can you name?

• An arithmetic operator is a symbol


that will perform an operation on numbers
• + is an example of an arithmetic operator
that uses two numbers, e.g. 5 + 2
• How many arithmetic operators can you name?
Data types and operations
Unit 9 Key programming concepts

Starter
• What is a data type?
The kind of values that can be used in a data item
• How many can you name?
integer, real / float, char / character, string, Boolean
• An arithmetic operator is a symbol that will perform
an operation on numbers
• + is an example of an arithmetic operator that uses
two numbers, e.g. 5 + 2
• How many arithmetic operators can you name?
+, -, *, /, ^, MOD, DIV
Data types and operations
Unit 9 Key programming concepts

IGCSE pseudo-code
• Throughout these lessons, computer code will be
written using IGCSE pseudo-code
• Sometimes the syntax may be different
or not possible in the programming
language you are studying
• Sometimes examples will
demonstrate a concept in a
real language – this will be
in Python unless
stated otherwise
Data types and operations
Unit 9 Key programming concepts

Using variables in a program


• You have probably used variables in many different
ways in programs you have already written
numberOfStudents ← numberOfStudents + 1
circleArea ← 3.142 * radius^2
WHILE NOT found…
answer ← 'Y'
OUTPUT studentName
• What are the data types of each of the
above variables?
Data types and operations
Unit 9 Key programming concepts

Data types
• Variables will typically be one of the following types:
• Integer, Boolean, real, char or string

• For the variables given, the data types would be:


• numberOfStudents Integer
• circleArea Real
• found Boolean
• answer Char
• studentName String
• The data type used will determine the amount of
memory that needs to be allocated for the variable
Data types and operations
Unit 9 Key programming concepts

Definitions of data types


Typical amount of
Data type Type of data
memory
Whole number such as 156, 0 -
integer 2 bytes
54
Number with a fractional part
real 4 bytes
such as 1.5276, -68.4, 20.0
A single ASCII character such
char 1 byte
as A, b, 3, ! or space
Zero or more characters –
1 byte per character in the
string these are put inside quote
string
marks – e.g. "Thank you"
Theoretically just one bit,
Can only take the values True
Boolean but in high level languages
or False
often one byte
Data types and operations
Unit 9 Key programming concepts

Declaring variables
• Before using variables they should be declared
• Declaring a variable gets it ready for use in the program
• The data type is given to the variable – the programming
language (compiler) can check that it is being used correctly
• Examples of declaring and using variables
DECLARE sidesInShape : INTEGER
DECLARE name : STRING
sidesInShape ← 4
Name ← "Jody"
Data types and operations
Unit 9 Key programming concepts

Constants
• As well as variables, you can define constants
in a program
CONSTANT Pi ← 3.14157926535
CONSTANT VAT ← 0.2
CONSTANT MaxPlayers ← 6

• Constants are often shown in uppercase


• Words are separated with an underscore, e.g. MIN_AGE
• This is known as snake case

• Why declare a constant instead of a variable?


• Can a constant ever change its value?
Data types and operations
Unit 9 Key programming concepts

Constants
• Why declare a constant instead of a variable?
• This prevents the value from being changed accidentally by a
part of code
• It shows a programmer that the value should stay the same
throughout the program
• Can a constant ever change its value?
• A constant cannot be changed when the program is running
• A constant can be changed by a programmer before the
program is compiled or translated
Data types and operations
Unit 9 Key programming concepts

Input and output statements


• Most programs accept data from the user, process it
in some way, and output a result
OUTPUT "How many hours a night do you
sleep?"
INPUT hoursPerNight
hoursPerWeek ← hoursPerNight * 7
OUTPUT hoursPerWeek
Data types and operations
Unit 9 Key programming concepts

INPUT statement
• In many high-level programming languages such as
Python, an input statement can have a prompt for
the user:
firstName = input("What is your name? ")
• This statement first displays the message “What is
your name?” and then waits for the user to enter
some text and press Enter
• The response is then assigned to the
variable firstName
Data types and operations
Unit 9 Key programming concepts

Arithmetic operators
• The operators +, -, * and / are used for addition,
subtraction, multiplication and division
• ^ is used for an exponent (power of)

• The operator DIV is used for integer division, also


known as quotient
• MOD (modulus) is used to find the remainder when
dividing one integer by another
• What is the result of the following operations?
• weeks ← 31 DIV 7
• daysLeft ← 31 MOD 7
Data types and operations
Unit 9 Key programming concepts

MOD and DIV


• weeks ← 31 DIV 7
• The variable weeks is assigned the value 4
• This is because 7 goes into 31 four times (remainder 3)

• daysLeft ← 31 MOD 7
• The variable daysLeft is assigned the value 3
• This is because the remainder after the division is 3
Data types and operations
Unit 9 Key programming concepts

Orders of precedence
• Remember BIDMAS
• Brackets
• Indices
• Division
• Multiplication
• Addition
• Subtraction
• Calculate: x ← (5 – 2) + (16 – 6 / 2)
y ← 7 * 3 + 10 / 2
Are brackets needed in the first expression?
Data types and operations
Unit 9 Key programming concepts

Worksheet 1
• Now complete Task 1 on Worksheet 1
Data types and operations
Unit 9 Key programming concepts

Strings and numbers


• Strings and numbers used in calculations are
represented differently in binary
• The string "17" is represented in binary as
• 0011000100110111
• The integer 17 is held as in binary as
• 00010001
Data types and operations
Unit 9 Key programming concepts

Inputting numeric variables


• Inputs from the user are strings
• Therefore if you are inputting an integer or real
number, you have to convert it before it can be used
in a calculation
• This doesn’t matter when writing pseudocode for IGCSE, but it
is important when writing in a real language such as Python
OUTPUT "Please enter number of tickets required: "
INPUT tickets

• In Python:
tickets = int(input("Please enter number of tickets required: "))

This converts the string input into an integer


Data types and operations
Unit 9 Key programming concepts

Concatenating strings
• Concatenating means joining together
• The + concatenate operator is used to join together strings
firstname ← "Rose"
surname ← "Chan"
fullname ← firstname + " " + surname
OUTPUT fullname
• What will be output?
• What will be output by the program?
x ← "6" + "3"
OUTPUT x
Data types and operations
Unit 9 Key programming concepts

Concatenating strings
firstname ← "Rose"
surname ← "Chan"
fullname ← firstname + " " + surname
OUTPUT fullname
• What will be output? "Rose Chan"
• What will be output by the program
x ← "6" + "3"
OUTPUT x
"63"
Data types and operations
Unit 9 Key programming concepts

String handling functions


Function Example Result
LENGTH(str) word ← "Algorithm" 9
OUTPUT LENGTH(word)
SUBSTRING(str, start, length) OUTPUT(word,3,6) "gorith"
LCASE(str) LCASE(word) algorithm
UCASE(str) UCASE(word) ALGORITHM

• What will be the values of a, b


and c below?
zooName ← "London Zoo"
a ← LEN(zooName)
b ← SUBSTRING(zooName,2,3)
c ← LCASE(zooName)
d ← UCASE(zooName)
Data types and operations
Unit 9 Key programming concepts

String handling functions


Function Example Result
LENGTH(str) word ← "Algorithm" 9
OUTPUT LENGTH(word)
SUBSTRING(str, start, length) OUTPUT(word,3,6) "gorith"
LCASE(str) LCASE(word) algorithm
UCASE(str) UCASE(word) ALGORITHM

• What will be the values of a, b


and c below?
zooName ← "London Zoo"
a ← LEN(zooName) 10
b ← SUBSTRING(zooName,2,3) ond
c ← LCASE(zooName) london zoo
d ← UCASE(zooName) LONDON ZOO
Data types and operations
Unit 9 Key programming concepts

Uppercase and lowercase


• In Python, a string can be converted to uppercase or
lowercase letters as follows:
phrase1 = "Good morning"
phrase2 = "HAPPY BIRTHDAY"
print(phrase1.upper()) #"GOOD MORNING"
print(phrase2.lower()) #"happy birthday"
• What is the output of the following program?
a ← "The quality of mercy is not strained."
b ← LENGTH(a) – 30
c ← SUBSTRING(a,1,b)
d ← SUBSTRING(c,1,LEN(c)-1)
OUTPUT d
Data types and operations
Unit 9 Key programming concepts

String handling
• What is the output of the following program?
a ← "The quality of mercy is not strained."

b ← LENGTH(a) – 30 b ← 37 - 30
b ←7
c ← SUBSTRING(a,1,b)
c ← "The qua"
d ← SUBSTRING(c,1,LEN(c)-1)
d ← "The qu"

OUTPUT d "The qu"


Data types and operations
Unit 9 Key programming concepts

Using comments
• You should use comments in your programs:
• to describe the purpose of the program
• to state the author of the program
• to explain what the code does

• Pseudo-code comments start with a // symbol


• In Python, comments start with #
• In Java and C#, comments start with //
• In VB, comments start with '

• Comments are ignored when your program is


translated to machine code and executed
Data types and operations
Unit 9 Key programming concepts

Worksheet 1
• Now complete Task 2 and Task 3 on Worksheet 1
Data types and operations
Unit 9 Key programming concepts

Plenary
• In pairs give:
• 5 different data types
• 4 arithmetic operators
• 3 functions used for string operations
• 2 arithmetic operators used for integer division and remainder
• 1 concatenate operator
Data types and operations
Unit 9 Key programming concepts

Plenary
• 5 different data types
• Integer, real / float, char / character, string, Boolean

• 4 arithmetic operators +, -, *, /, ^
• 3 functions used for string operations
• LENGTH(string), LCASE(string), UCASE(string),
SUBSTRING(string, start, length)
• 2 arithmetic operators used for integer division and
remainder
• DIV (integer division), MOD (remainder)

• 1 concatenate operator +
Data types and operations
Unit 9 Key programming concepts

Copyright

© 2021 PG Online Limited

The contents of this unit are protected by copyright.

This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.

Licence agreement

This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.

The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.

You might also like