General
Programmers in Pascal/Lazarus must decide WHAT information (or
object) is needed in their programs and what TYPE it needs
to be.
Each type of data consumes a different amount of MEMORY (ie. bytes)
A VARIABLE could be thought of as a container for information
(=data). In designing a container (ie. type casting), we make decisions
about what sorts of values can fit into the container. In addition,
we place a uniquely identifyable NAME (=identifier) on the outside
of the container. We use this name when we want to refer to the container,
or it's contents.
Scalar data is single -valued data - that is, each
scalar container is designed to store a single value - each time you
place a value into a scalar container, you erase the previous contents
of the container. We will consider compound data types a little later
in the course.
VARIABLE DECLARATION:
A DECLARATION is a claim of memory - it sizes the memory and points to it with the nominated name:
eg: var total : integer;
This declaration, placed in the STORE of the project or process, claims 2 bytes of memory to be used as a
container for positive or negative whole numbers. The container will
be known as 'total'
note: at compile time, the compiler changes the name 'total' into
a memory location (hex-segmented) in the object code version of the
program.
VARIABLE ASSIGNMENT:
total := 45
This variable assignment places the integer value '45' in
the container known as 'total'. Prior to the assignment, there was
an UNKNOWN integer value there - WARNING: it is bad form to use containers
without assigning them values because until you place a value there,
you cannot be sure what is stored there (and could be in for a nasty
surprise when you use it).
:= means takes on the value of (or recieves the value of)
answer := (25*45)-5;
sum := num1 + num2 + num3
Simple (scalar) Lazarus Data Types
BYTE
a whole number in the range 0 --> 255 (or in hex $00 --> $FF), use
1 byte of RAM each
INTEGER
a whole number (either + or -) in the range -32768 --> 32767 ($0000
---> $FFFF in hex), use 2 bytes of RAM each. Numbers represented using
two's compliment form.
SHORTINT
whole numbers in the range -128 to 127, use 1 byte of RAM
LONGINT
whole numbers in the range -2147483647 to 2147483647, uses 4 bytes
of RAM
WORD
a whole number in the range 0 to 65535, uses 2 bytes of RAM
CHAR
a symbol ASCII symbol (usually in quotes eg. 'h') (or #27 meaning
ASCII number 27), use 1 byte of RAM. note: 'gidday' is a sequence
of 6 values of type char
BOOLEAN
either true or false, uses 1 byte of RAM each
REAL
numbers with a decimal place in range -10^38 --> 10^38 (eg -3.7E5),
includes fractional quantities down to 10^-38, use 6 bytes of RAM
each
SINGLE
a number with fractional component, requires 4 bytes RAM, that has
7-8 signifigant digits
DOUBLE
a number with fractional component, requires 8 bytes RAM, that has
15-16 signifigant digits
EXTENDED
a number with fractional component, requires 10 bytes of RAM that
has 19-20 signifigant digits
COMP
a number with fractional component, requires 8 bytes of RAM that has
19-20 signifigant digits
POINTER
memory addresses used to point to other variable spaces
Operating on simple types
UNARY operators require only ONE argument (eg -5, SQRT(x))
BINARY operators require TWO arguments (eg. 4/5, x MOD y)
ORDER CONVENTION:
UNARY OPS
MULTOPS (* / MOD, AND SHL etc)
ADDOPS (+ - or etc)
RELOPS (< > etc.)
Operating on Simple types(specifically)
BYTES
UNARY OPS: #, +/-, NOT, ODD, ABS, ORD, SUCC, PRED, SQR,
CHR, CHAR, SQRT, RANDOM, SIZEOF, INTEGER, INC
BINARY OPS: +, -, *, /, DIV, MOD OR, AND, XOR, SHL, SHR,
<=, >, >=, <;, =
NOTES: answer MUST always be in range of data type (0 --> 255) else
a LOGICAL error will result (ie. program will report wrong answer
as overflow from byte 'falls off')
CHARS
UNARY OPS: ^, BYTE, ORD, INTEGER, SUCC, PRED, UPCASE,
SIZEOF
BINARY OPS: >, <, =, <=, >=, <>
NOTES: answers to binary character expressions usually BOOLEAN (ie.
TRUE or FALSE). All comparisons in character data are performed in
ASCII (ie. 'a' < 'A')
BOOLEAN
UNARY OPS: NOT, BYTE, INTEGER, ORD, SUCC, PRED, CHAR,
SIZEOF
BINARY OPS: AND, OR, XOR, <, >, <>, >=, <=
NOTES: answers usually TRUE or FALSE. The VALUES TRUE and FALSE are
NOT character or string data.
INTEGER
UNARY OPS: same as for BYTE with inclusion of +/-
BINARY OPS: same as for BYTE, with the addition of HI, LO, SWAP
NOTES: Answers to INTEGER expressions must return answers in the
INTEGER range (ie. -32768 .. 32767) else either a RUNTIME error will
result or the answer will be junk (again, either overflow from the
container will "fall off" or we will exceed MAXINT and enter the negative
domain). The HI, LO and SWAP are integer specific operations and rely
on the integers internal representation.
Standard integers are represented with TWO BYTES
REAL
UNARY OPS: +/-, ABS, SQR, SQRT, TRUNC, ROUND, INT, FRAC,
LN, EXP, COS, SIN, ARCTAN, SIZEOF, RANDOM
BINARY OPS: +, -, *, /, <, >, <=, >=, <>
NOTES: problems with memory representations and rounding in calculations
often lead to answers being a little inaccurate - therefore they are
sparingly used in comparisons if both values are calculated. RUNTIME
errors occur if the answers to real expressions fall outside the real
range
Scalar Conversion Between Types(or Typecasting)
You can force compatible data into a new type by typecasting it
eg.
Var
c : char;
b : byte;
i : integer;
r : real;
Begin
c := #i;
b := ord(c);
r := i * 1.0;
i := integer(ord(c));
c := #(byte(trunc(r)));
Scalar Data in Lazarus