Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Built-in data types

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Arithmetic operators Constant Definitions All programming languages have built in data types that are used when declaring variables (though not all programming languages have variables - don't worry about this yet though!). Some very 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 FALSEAlternatively 1 or 0Alternatively 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: hello Barry! you are 56 years old 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: Age Name Gender Height(metres) Date of Birth license (Do they have a driver license) 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: Name Age Gender 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 correct, which are wrong and why? dim colour as string dim wheelNum as integer dim topSpeed as single dim hasElectricWindows as char 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 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: Prevents mistakes in code and calculations at run time Makes for smaller and faster programs 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: Please insert number 1: 2 Please insert number 2: 3 Please insert number 3: 7.8 multiplied together = 46.8 added together = 12.8 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. Strongly means that you can't add a string to a integer, even if the string contains a number. Weakly typed allows you to add a string to an integer if the string contains a number Weak Typing Strong Typing Pseudocode a = 2 b = '2' concatenate(a, b) # Returns '22' add(a, b) # Returns 4 a = 2 b = '2' concatenate(str(a), b) # Returns '22' add(a, int(b)) # Returns 4 concatenate(a, b) # Type Error add(a, b) # Type Error Languages Visual Basic, Perl, PHP, Rexx Java, C, C++, Python