Information Technology Topic
Data Types
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- Integer holds whole numbers, real (floating point) holds numbers with a fractional part, and character holds a single symbol
- A literal is a value written directly in the algorithm, such as 42, 3.5, 'K' or "Kingston"
- In Pascal-style arithmetic, + - * give an integer from two integers and a real if either is real, but / always gives a real
Section 1What a data type is
A data type says what kind of value something is, and so what can be stored in it and done with it. A number of pupils, a price and a grade letter are different kinds of data, so they need different types. This topic covers the types in the syllabus: integer, real (also called floating point) and character, together with literals. Here's the trick: ask what the value is before choosing the type. Is it counted, measured, or a single symbol?
Section 2Integer: whole numbers
An integer is a whole number with no fractional part. It can be positive, negative or zero: 45, 0 and -8 are all integers. Integers suit things that are counted: the number of pupils in a class, the number of zinc sheets ordered, or a counter that goes 1, 2, 3. In Pascal-style code the type is called integer.
Section 3Real (floating point): numbers with a fractional part
A real number can have a fractional part: 3.75, 450.5 and 72.5 are reals. Real numbers are also called floating point numbers, after the way a computer stores them. Reals suit things that are measured or may need cents: a price, a height, an average mark. A real is written with a decimal point. So 12.0 is a real, even though its value is a whole number. Real values may be stored approximately, so a result such as 10 / 3 is shown as a long, rounded decimal. In Pascal-style code the type is called real.
Section 4Character: one symbol
A character is a single symbol: a letter, a digit, a punctuation mark or a space. In this topic a character literal is written in single quotes, like 'A', 'B' or 'Y'. Characters suit a grade letter or a Yes/No answer typed as Y or N. Easy to mix up: '9' is the character nine. It is a symbol, not the number 9, and it is not used in arithmetic as a number. The integer 9 is a different value. In Pascal-style code the type is called char.
Section 5Literals: values written in the algorithm
A literal is a value written directly in the algorithm. 42 is an integer literal, 3.5 is a real literal, 'K' is a character literal, and text in double quotes such as "Kingston" is a string literal, a row of characters. In Store 42 in age, the literal is 42 and the variable is age. The literal is the value itself. The variable is the named place where it is kept. Easy to mix up: 'A' in quotes is a character value, but A without quotes is a name. Quotes tell the computer that the symbol is data and not the name of a variable. This course's pseudocode uses single quotes for one character and double quotes for text; real Pascal code uses single quotes for both, so follow the style your teacher uses.
Section 6Choosing the right type
Match the type to the kind of value. A number of tickets sold is counted, so use an integer. A ticket price of 450.50 has cents, so use a real. A grade letter is one symbol, so use a character. An average of whole marks also needs a real. The average of 70 and 75 is 72.5, and an integer cannot hold the .5. Pascal-style declarations show the types by name: var tickets : integer; price : real; grade : char;
Section 7The type of an arithmetic result
In the Pascal-style rules this course follows, + - * on two integers give an integer. If either value is a real, the result is a real. The / operator always gives a real, even for two integers. So 7 + 3 is the integer 10. 4 * 2.5 is the real 10. 7 / 2 is the real 3.5. And 6 / 3 is the real 2, not an integer. Watch out: a real result can look like a whole number and still be a real. Check the operator and the types of the two values, not just how the answer looks.
Examples
Classify each of these literals as an integer, a real, a character or a string literal: 45, 3.75, 'K', "Kingston", 0, -8, 12.0, '9'. Then explain (a) why 12.0 is not an integer and (b) why '9' is not the number 9.
45 is an integer. 3.75 is a real, because it has a fractional part. 'K' is a character, a single symbol in single quotes. "Kingston" is a string literal, text in double quotes. 0 is an integer. -8 is an integer, because a negative whole number is still whole. 12.0 is a real. '9' is a character. (a) 12.0 is written with a decimal point, so it is a real, even though its value is a whole number. An integer has no decimal point. (b) '9' is in quotes, so it is the symbol nine and not a number. The integer 9 can be used in arithmetic, such as 9 + 1 = 10, but the character '9' cannot.
A school in Mandeville sells concert tickets. A program runs these lines: Store 12 in tickets Store 450.5 in price Store tickets * price in total Store tickets + 3 in more Store total / tickets in each Store 6 / 3 in half Using the Pascal-style rules, give the type and value of each variable. Then choose a suitable type for a grade letter held in a variable called grade.
tickets is an integer with value 12, because 12 is a whole number. price is a real with value 450.5. total is tickets * price = 12 * 450.5 = 5406. One value is a real, so the result is a real: real, 5406. more is tickets + 3 = 12 + 3 = 15. Both values are integers, so the result is an integer: integer, 15. each is total / tickets = 5406 / 12 = 450.5. Division always gives a real: real, 450.5. half is 6 / 3 = 2. Division always gives a real, even from two integers: real, 2. A grade letter is a single symbol, so grade should be a character, for example 'B'.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 2, Specific Objective 4 · Syllabus scope
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 3, Ordinal types (the Integer type), checked against (https://www.freepascal.org/docs-html/ref/refsu4.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 3, Real types, checked against (https://www.freepascal.org/docs-html/ref/refsu5.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 3, Character types (Char, written in single quotes), checked against (https://www.freepascal.org/docs-html/ref/refsu6.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 12, Arithmetic operators (integer or real results; / always real), checked against (https://www.freepascal.org/docs-html/ref/refsu44.html) · Facts
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Chapter 3, An Informal Introduction to Python, numbers (language-neutral: whole numbers and fractional numbers; mixing the two gives a fractional result), checked against (https://docs.python.org/3/tutorial/introduction.html) · Facts