S2 Computer Literacy

advertisement
CLSMSS
S3 Computer Literacy: Part 2: Data Types
S3 Computer Literacy
Part 2: Data Types
 In Pascal, data can be classified into different data types. A variable must be
declared as a specific data type before they can be used in a program.
1. integer:
It can contain integers (whole numbers) from -32768 to 32767. If
the value is out of range, it will not be stored in the correct value.
2. real:
It is a number with decimal point, e.g. 3.1415. It can also be
written in scientific notation, e.g. 1.23E4, i.e. 1.23x104.
It stores exactly one character. E.g. 'a', 'Z', '+', '8', etc.
Note that '8' is a character while 8 is a number (integer), and
3. char:
uppercase letters and lowercase letters are different characters.
4. string:
It stores a sequence of characters. The length ranges from 0 to
255. E.g. 'Pascal', 'Computer Studies', ''(null string),
etc. Note that, char and string must be enclosed in single quotes.
5. Boolean: Its value can be either TRUE or FALSE.
 Understanding the range of an integer
Look at the following program:
program test1;
var x, y : integer;
begin
x := 20000;
y := x + x;
writeln(y)
end.
x and y are declared as integer.
x is assigned to be 20000.
y is assigned to be x + x.
Output the value of y to the screen.
What do you expect to be seen on the screen? 40000?
and run it to see the result.
 Real numbers
Try the following program:
program test2;
var z : real;
begin
z := 3.1415;
writeln(z)
end.
Try typing the program
z is declared as real.
Output the value of z to the screen.
What is the result you can see?
2002-2003
P. 1/3
CLSMSS
S3 Computer Literacy: Part 2: Data Types
 Different notation of real numbers:
Fixed Point Notation
Scientific Notation
(Used in daily life)
(Used in science)
12.34
1.234 x 101
Floating Point Format
(Displayed on the screen)
1.2340000000E+01
0.000789
7.89 x 10-4
7.8900000000E-04
-999.0
-9.99 x 102
-9.9900000000E+02
12345.6789
1.0100000000E-01
-5.0505000000E+05
 Length of strings
The length of a char is always 1. However, the length of string can be 0 to 255.
String
Length
Remark
'Pascal'
6
'Computer Studies'
16
Blank is also a character
'12345'
5
Digits are characters.
'@#@#$$$'
7
Symbols are characters.
''
0
Null String
To get the length of a string, we use “length(s)”, where s is a string .
 To join two strings, we use “+”.
program JoinString;
var x, y : string;
begin
x := 'Peter';
y := 'Hi, ' + x;
writeln(y)
end.
The output is: “Hi, Peter”
Look at the following example:
joining 'Hi, ' and 'Peter'.
 Assigning value to a variable
program Testing;
var mark : integer;
name : string;
sex : char;
weight : real;
fail : Boolean;
begin
mark := 76;
name := 'Chan Tai Man';
sex := 'M';
weight := 43.5;
fail := TRUE;
2002-2003
P. 2/3
CLSMSS
S3 Computer Literacy: Part 2: Data Types
Class: ________ Class Number: ________ Name: __________________________
Exercise
1. State the type of the following values:
Data
Type
789
'?'
'TRUE'
2.236
'1.449'
FALSE
2. Fill in the following table for different notation of real numbers:
Fixed Point Notation
Scientific Notation
Floating Point Format
123.456
0.0
-0.00000000001
6.0200000000E+08
-5.4321000000E-03
3. Suggest a suitable identifier name and a data type for each of the following:
Data to store
Identifier Name
Data Type
Student Name
StudentName / Sname / sn
string
Class Name
Class Number
Average Mark
Telephone Number
HKID Number
4. Write on the next page the declaration of the variables in question 3.
2002-2003
P. 3/3
Download