Data Types and Programming Techniques - OCR GCSE

The Use Of Data Types

The data type determines what type of value a variable will hold. A variable must have a data type. Data types:

  • Integer - whole numbers, eg. 34

  • Float - decimal numbers, eg. 6.7

  • Character - a single alphanumeric character, eg. A

  • String - one or more alphanumeric characters, eg. Hello

  • Boolean - True/False

Different data types have different limitations. Integers and floats cannot be concatenated. Numbers in strings cannot have mathematical operations applied to them. 


Casting

Changing the data type of a variable. Eg. from string to integer so that a mathematical operation can be applied.


Basic string manipulation

You can manipulate a string to provide information or alter its contents.

  • Length - Using the ‘len’ statement you can find the length of a string as an integer, eg. len(hello) would be 5

  • Character position - Finding out which character is at a certain position in a string, eg. hello[2] would give e

  • Upper or lowercase - Changing all characters in string to upper or lower case. Eg. word = “Hello”     word = word.lower

  • Concatenation - joining two strings together or adding more characters to the end of a string. Eg. sentence = wordOne + “ “ + wordTwo


Basic file handling operations

Once a program has finished or closed all of the data it has stored or processed will be lost. This can be prevented by storing the data in a file.

Files have two modes of operation:

  • Read from - file is opened so data can be read

  • Write from - file is opened so data can be written

An item of data from a file is called a record.


Opening and Closing files

A file must be opened before it can be read or written to. To be opened it must be referred to by its identifier. 


Reading from a file

Data is read line by line. Can be read into a variable or more commonly an array.


End of file

An ‘endOfFile()’ statement is used so that the program knows when the last record has been read.


Writing to a file

Data is written into a file one line at a time using the ‘writeLine’ statement.


Using Records to store data

Data is often stored in a database. Data in a database is stored as records which are stored as files. An attribute is one item of data, records usually have more than one attribute. Eg. title, forename, surname, email address


Using SQL to search for data

Databases use Structured Query Language (SQL) as their programming language.

Data can be retrieved using the commands ‘select’, ‘from’ and ‘where’.

The ‘like’ command can be used to find matches for incomplete words.

The boolean operators ‘and’ and ‘or’ can be used to retrieve data.


Using Arrays

To save time programmers used arrays to store lots of related data instead of storing each piece in a separate variable. An array has a collection of elements, each element has a position in the array and stores a value. Data in an array must be of the same data type. This means all data is stored under one identifier.


Declaring Arrays

An array must be declared before it can be used. To declare an array it must have an identifier and a size.


Assigning Values to an Array

To assign a value to an element you would use the element's position. Eg. score[0] = 100

Value in an element can be changed at any time by assigning another value to the element.

Values are retrieved using the element’s position.


Two-dimensional Arrays

A two dimensional array can hold more than one set of data. Acts like a table with rows and columns. Declared using two values, the number or rows and columns. Eg. ‘array score = [1,9]’ would give an array with two rows and ten columns.

Data is assigned or retrieved using the element’s row and column number.


Subprograms

Subprograms are small programs within a larger program. They perform specific tasks and save time if that task needs to be done multiple times throughout the program. The two types of subprogram are procedures and functions.

Benefits:

  • Small in size so easier to write, test and debug. Easier to understand.

  • Can be saved as separate modules and used in other programs, saves time.

  • Can be used repeatedly in the main program but only has to be written once, making the program shorter.


Procedures

Performs a specific task, once completed the main program continues. A procedure is run by calling it, to call it you use its name and any values it needs.


Function

Same as a procedure but it manipulates data. Eg. turns Fahrenheit into Celsius. Run by calling it, to call it you use its identifier, the value passed into the function and the variable the function will return a value to.

Many languages have built in functions such as ‘int’ (converts strings/float into integers), ‘str’ (converts numbers into strings) or ‘asc’ (finds ASCII number of a character)

Some languages allow functions to be added from external files called libraries. Libraries extend functionality of a language.


Random Number Generation

Random numbers are used to produce a number within a range. This can be used in games such as dice rolling or can be used to create strong passwords or to transmit data across a network without collisions.