• Keine Ergebnisse gefunden

SIMPLE DATA TYPES

Im Dokument Alpha PASCAL (Seite 89-93)

BEGIN { MAX }

7.1 SIMPLE DATA TYPES

scalar data would be made (SET, ARRAY,

Simple data types can either be the pre-declared simple data types (INTEGER, REAL, BOOLEAN, and CHAR), or they may be types defined by you. If defined by you, a simple data type is either a scalar type or a subran~e of another, already defined scalar data type.

7.1.1 INTEGER

Integers are whole numbers (that is, numbers with no fractional part).

AlphaPascal allows you to use integers in the ran~e of -32767 through 32767.

They are stored by the computer as one-word, signed 2's complement binary numbers. These are integers:

32000

o

1 -450 MAXINT

+56

(Remember that the pre-declared constant MAXINT is the largest integer that AlphaPascal can represent, 32767.)

The standard identifier INTEGER designates the integer data type. For example:

VAR Ellipse, Counter, Control: INTEGER;

The operators that have been defined for integers are: addition (+);

subtraction or sign inversion (-); multiplication (*); integer division--that is, divide and truncate-- (DIV); moduLus (MOD); the set membership operator IN; and, the reLationaL operators. Using other operators (for exampLe, the reaL division operator, I) on integers causes the compiler to generate an error message.

There are many functions that accept INTEGER arguments. (See Chapter 12,

"MathematicaL Functions," for a list of the trigonometric, hyperboLic trigonometric, and mathematicaL functions.)

Two other functions often used on INTEGER data are the PRED and SUCC functions. PRED returns the predecessor eLement of the data type; SUCC returns the successor eLement of the data type. For exampLe, given three variabLes, ONE, TWO, THREE of type INTEGER, and ONE

=

1, TWO

=

2, and THREE

=

3: PRED(TWO) returns 1; SUCC(TWO) returns 3. (See Sections 11.1.6 and

11.1.8 for information on PRED and SUCC.)

7.1.2 REAL

ReaL numbers are decimaL numbers that may contain a fractionaL part. As noted in Section 5.6, "Notation," wecan represent reaL numbers either in decimaL notation or in scientific notation. These are reaL numbers:

9.'3 -56.7812 7.03E+5 +45.0 1.03E-3

The computer stores reaL numbers as three-word fLoating point numbers significant to 11 digits (12 for reaL numbers in which the fractionaL part is zero or Less than 1E12), with an exponent range of roughLy 1E-37 to 1E37.

The standard identifier REAL designates the reaL number data type. For exampLe:

VAR Mean, Median, Variance : REAL;

The operators defined for reaL numbers are: addition (+); subtraction and sign inversion (-); muLtipLication (*); reaL division (/); and, the reLationaL operators. Many functions accept REAL numbers as arguments.

Note that you may not use the PRED and SUCC functions or the set membership operator IN on REAL data.

7.1.3 BOOLEAN

The BooLean data type contains two eLements: TRUE and FALSE. These eLements are ordered so that FALSE < TRUE. (And, SUCC(FALSE) returns TRUE.) FALSE and TRUE are pre-decLared constants. A BooLean variabLe represents a LogicaL true or faLse vaLue. For exampLe:

IF Month = ApriL THEN Spring := TRUE

In the statement above, Spring is a BooLean variabLe that can assume the vaLues TRUE or FALSE.

To designate a BooLean data type, use the standard identifier BOOLEAN. For exampLe:

VAR Query, FemaLe, EmpLoyee: BOOLEAN;

The operators defined for BooLean data are: AND, OR, and NOT. These are caLLed BooLean operators, and produce a BooLean resuLt. For exampLe:

X AND Y

gives a resuLt of TRUE if both X and Yare TRUE, or FALSE if either X or Y (or both) are FALSE.

When we use the reLationaL operators on INTEGER, REAL, CHAR, or STRING data types, the resuLt is aLways of type BOOLEAN.

You may use the PRED and SUCC functions on data of type BOOLEAN, and you may use the set membership operator, IN. You may aLso use the ORD function:

ORD(FALSE)

=

0 ORDCTRUE) = 1

7.1.4 CHAR

The computer recognizes a specific set of characters that it can represent.

The eLements of this set are ordered; for exampLe, A < B < C... In the case of the ALpha Micro computer, this ordering is calLed the "ASCII collating sequence," and the set of characters is caLled the "ASCII character set."

(For a list of the ASCII characters, see Appendix B, "The ASCII Character Set.")

A CHAR variabLe contains one ASCII character. To indicate an element of CHAR data type, enclose it in singLe quotes. For exampLe:

VAR MenuChoice CHAR;

MenuChoice := 'A';

The reLationaL operators have been defined for use on CHAR data. Remember that A < B because of their position in the ASCII coLLating sequence. You may also use the set membership operator, IN on data of type CHAR.

To designate data as type CHAR, use the CHAR standard identifier.

or:

VAR InitiaL: CHAR;

TYPE Character

=

CHAR;

~ Item : Character;

Because CHAR is a non-REAL scaLar type, you can use the SUCC and PRED functions to identify predecessor and successor eLements of the type. For example:

PRED('B')

returns an 'A'. You can aLso use the ORD function to determine the position of the character in the ASCII character set. (For more information on PRED, SUCC, and ORO, see Chapter 11, "MisceLlaneous Functions and Procedures.")

NOTE: Remember that CHAR data is only one ASCII character. Another standard data type exists, STRING, which reprp.sents a collection of CHAR data. For example: 'A' is CHAR data, but 'ABCD' is STRING data. For information on STRING, see Section 7.2.3, "STRING."

7.1.5 User-Defined Scalar

Pascal allows you to define your own scaLar types. To do so, use the type declaration statement. You wiLL supply the name of the data type, and the eLements of which it is composed. For example:

TYPE Spectrum

=

(Violet,Blue,Green,Yellow,Orange,Red);

Just like any other scaLar type, your data elements. This ordering is refLected by the elements in the type declaration statement.

statement above, Violet < BLue < Green, and so on.

use a variable of the data type you have defined.

type consists of ordered order in which you List the For example, given the You can then decLare and For exampLe:

VAR Colors : Spectrum

IF CoLors = Red THEN WarmCoLor := TRUE;

The relational operators have been defined for user-defined scalar types, and return a Boolean result. Internally, the computer stores each of these eLements as an integer vaLue. (For example, in the example above VioLet is 0, Blue is 1, and so on.)

You may not use scalar types in I/O operations. For example, this statement is ill ega l :

WRITE (Ye llow)

if Yellow is an element of a user-defined scalar type. However, you could say something like:

.!£.

Colors

=

Yellow THEN WRITE('Yellow');

Note that Colors is a variable, but Yellow is a constant of the scalar type Spectrum (just as the number 2 is a constant of the scalar type INTEGER).

You may only use relational operators and the set membership operator, IN, on an element of a user-defined scalar type.

NOTE: Rather than using a type declaration foLlowed by a variable declaration, you may combine both statements into one variabLe declaration when defining your own data types. For example:

VAR WaveLengths,Colors (Violet,Blue,Green,Yellow,Orange,Red);

However, if you are.going to have more than one variable declaration that declares variables of that type, you must have a separate type declaration statement instead.

You may use the ORO, PRED and SUCC functions on user-defined scalar types.

For example, given our example above:

ORD(Violet)

=

0

ORO (Blue) = 1

SUCC(Violet)

=

Blue

PRED(Orange)

=

YeLLow

7.1.6 User-Defined Sub range

Pascal allows you to define a subrange of a previously defined data type.

For exampLe, given the data type Spectrum above, suppose you want a variable to only access the first three coLors eLements of that type, VioLet, BLue, and Green. You couLd define a subrange scaLar type:

TYPE CoLdCoLors

=

Violet •• Green;

You may define a subrange of any user-defined or standard scalar type except type REAL. Use the type declaration statement in this format:

TYPE Type-name

=

lowerLimit •• upperlimit;

The symbols " " teLL PascaL UpperLimit and LowerLimit are the subrange. For example:

that you beginning TYPE Decimal

=

'0' •• '9';

are

and estabLishing a subrange.

ending eLements of the

teLLs PascaL that we want to define a type named DecimaL that can assume values in the range of '0' through '9' of the standard data type CHAR. We can then declare a variabLe of that type:

VAR Number : DecimaL;

NOTE: You may aLso directLy decLare a variabLe to of a subrange without using a type declaration statement. For exampLe:

VAR Number: '0' •• '9';

Im Dokument Alpha PASCAL (Seite 89-93)