A-level Computing/AQA/Paper 1/Fundamentals of programming/Data types
Most programming languages have built in data types that are used when declaring variables. Some common data types, and the ones you need to know for the exam, are as follows:
Type | Description | Memory Space | Example |
---|---|---|---|
Integer | a whole number from -2,147,483,648 through 2,147,483,647 | 4 bytes | 37,453 |
Byte | a whole positive number from 0 to 255 | 1 byte | 12 |
Real | Visual Basic does not use Real Numbers, instead it uses {Single} and {Double}, which both allow for decimal places | - | - |
{Single} | 1.5 x 10−45 to 3.4 x 1038 | 4 bytes | 1002.375 |
{Double} | 5.0 x 10−324 to 1.7 x 10308 | 8 bytes | 9997.775 |
Decimal | 7.9228 x 10−28 to 7.9228 x 1028 | 16 bytes | 3.8 |
Boolean | either TRUE or FALSE Alternatively 1 or 0 Alternatively Yes or No | 4 bytes (!) | TRUE |
Character | A single character | 2 bytes | j |
String | A collection of characters | A unicode string with a maximum length of 2,147,483,647 characters | cabbage |
Date/Time | There are several different types of date format that you can apply. 01/01/0001 to 12/31/9999 and times from 12:00:00 AM (midnight) through 11:59:59.9999999 PM | 8 bytes | 08/17/1924 14:34:23 |
Using these data types we can start to write a simple computer program:
dim name as string
dim age as integer
name = "Barry"
age = 56.3
Console.writeline("hello " & name & "! you are " & age & " years old")
But wait a second, this gives you an odd output, it says:
I told it that Barry was 56.3 years old! The reason is because I have used an integer to store the age and not a real (single or double) datatype, it therefore drops the decimal part. Integers, afterall, don't store decimal places!
Assignments
Depending on the datatype, we assign values in different ways:
- Integers, Bytes, Real, Singles, Doubles = Plain assignment without speech marks
exampleNumber = 7.65
- Boolean = Plain assignment without speech marks
paidMember = TRUE
- String, Char = Assignment with speech marks
name = "Henry"
- Date = Assignment with speech marks
doB= "12/12/45"
Exercise:Data types Using the correct datatype declare variables for a person's:
Answer: dim age as integer
dim name as string
dim gender as char 'OR dim gender as string
dim height as decimal
dim DoB as date
dim license as boolean
Write assignment statements for the following variables using yourself as an example:
Answer: name = "Peter" 'we must use speech marks for text, so we don't mistake the value for a variable
age = 56 'we don't need speech marks for numbers
gender = "m"
Which of the following declarations are correct, which are wrong and why? dim colour as string
dim wheelNum as integer
dim topSpeed as single
dim hasElectricWindows as char
Answer: dim colour as string 'CORRECT, also we could use a single to store the frequency
dim wheelNum as integer 'CORRECT
dim topSpeed as single 'WRONG, we don't need such precision, an integer would do
dim hasElectricWindows as char 'WRONG, a boolean would work better
Which of the following assignments are correct, which are wrong and why: name = Pete
age = "34"
height = twenty
electricWindow = True
Answer: name = Pete 'WRONG, we're assigning a string so we need to use speech marks
name = "Pete"
age = "34" 'WRONG, we're assigning an integer so we don't need speech marks
age = 34
height = "twenty" 'WRONG, height is numeric, so we need to assign a number without speech marks
height = 20
hasElectricWindow = True 'CORRECT assuming you made the change from the previous question
Give two reasons why is it important to get the correct datatypes:
Answer:
Write code that asks the user to insert three numbers with decimals, then outputs them (1) all multiplied together, and (2) added together. For example: Answer: dim num1, num2, num3 as single 'NOT integer, we need decimal numbers! You can use different identifiers
console.write("Please insert number 1:")
num1 = console.readline()
console.write("Please insert number 2:")
num2 = console.readline()
console.write("Please insert number 3:")
num3 = console.readline()
console.writeline("multiplied together = " & num1 * num2 * num3)
console.writeline("added together = " & num1 * num2 * num3)
console.readline()
|
Extension: Strongly and weakly typed languages You don't need to know this part for the exam, but it should help you understand why we need to convert. We have two types of programming language: strongly typed and weakly typed. Strong typing means that you can't add a string to a integer, even if the string contains a number. Weakly typed languages allow you to add a string to an integer if the string contains a number.
|