• Keine Ergebnisse gefunden

ELSE GCD:=GCD(n)m MOD n) ENDi

Im Dokument PASCAL-a6 USER'S GUIDE (Seite 88-91)

x ;

FUNCTION GCD;

(*

no parameter list needed here

*)

BEGIN

IF n=O THEN GCD:=m

ELSE GCD:=GCD(n)m MOD n) ENDi

To make your programs easier to follow, especially when doing top-down program-ming, you may find it useful to give FORWARD declarations for all your procedures and functions. Once you have done this, you can place the body declarations after the forward declarations in any order you wish.

FOR WARD is not a reserved word, so you may use it as an identifier in your program.

Unlike predefined identifiers, however, FOR WARD does not lose its original meaning as a directive when you define or declare it as an identifier. In such a case, each time FOR WARD appears, the context determines which of the two meanings (directive or identifier) applies.

NOTE

The Pascal-86 compiler does not check for non-standard forward references, so the FOR WARD directive is not required in Pascal-86 programs. However, you should use it if you wish your programs to be portable.

CHAPTER 7 EXPRESSIONS AND STATEMENTS

Statements in Pascal describe the actions to be performed on the data in your programs. Statements may include expressions, which denote rules for generating new values by applying operators to operands. The expressions are evaluated during program execution as required by the statements that contain them.

7. 1 Expressions

Expressions are combinations of operands-variables and constants of scalar or set types-and operators that use the operands to compute new values. An expression may include one or more matched pairs of parentheses which serve to group operands and operators as desired. You use function designators (references to functions) in the same way as operands; thus you may use functions, as well as operators, to operate on values. An error is caused if any variable or function you use as an operan<;l in an expression has an undefined value at the time the expression is evaluated. You will probably use expressions most often in assignment statements (7.2.1), which assign a newly computed value to a variable or a component of a variable.

Pascal provides four kinds of operators:

The arithmetic operators: addition, subtraction, negation, multiplication, division, and remainder. Results are of INTEGER,

• The Boolean operators: negation (NOT), disjunction (OR), and conjunction (AND). Results are of type BOOLEAN.

The set operators: union, intersection, and set difference. Result types are sets based on ordinal types.

• The relational operators: equality, inequality, ordering, set membership, and set inclusion. Results are of type BOOLEAN.

No operators producing new arrays, records, or files are defined. You assign new values to these by changing their scalar components individually. In addition, you may transfer the values of entire structures among variables associated with the same explicit type definition.

The relative precedence of operators determines which ones are applied first when an expression is evaluated during program execution. Operators are of four levels of precedence. From highest to lowest, they are

1. The negation operator (NOT)

2. The multiplying operators: multiplication or set intersection (*), division (j), division with truncation (DIV), remainder (MOD), and conjunction (AND) 3. The adding operators: addition, unary identity, or set union (+); subtraction,

unary negation, or set difference (-); and disjunction (OR)

4. The relational operators: equality (=), inequality

« »,

less than «), greater than (> ), less than or equal to or set inclusion (-<), greater than or equal to or set inclusion (:> ), and set membership (IN)

When an expression is evaluated upon execution of the statement that contains it, operators of highest precedence are applied first.

7-1

Expressions and Statements Pascal-86 User's Guide

The order in which the operands of a binary operator are evaluated (if they are subexpressions or function designators, or involve indexing an array or dereferencing a pointer) is undefined, so you should make no assumptions about this order. If the order of evaluation of subexpressions is important, put them in separate statements.

7. 1.

1 Expression Syntax

An expression may be composed of sub expressions called simple expressions, terms, and factors. Syntactically, factors are combined via multiplying operators to form terms; terms are combined via adding operators to form simple expressions; simple expressions are combined via relational operators to form expressions.

By definition, simple expressions, terms, and factors are always expressions, too. Thus a variable identifier is an expression; so is a constant.

The syntax of an expression is:

simple-expression [relational-op simple-expression]

where

relational-op is a relational operator.

simple-expression is a simple expression with the syntax:

[ sign] term [adding-op term] ...

Here, sign is a plus or minus sign used as a unary operator, adding-op is an adding operator, and term is given by:

factor [multiplying-op factor] ...

where

multiplying-op factor

is a multiplying operator as defined in 7.1.

is anyone of the following:

an entire variable, structured variable component, or refer-enced variable (5.4.2)

a named constant (5.2) a literal integer (3.3.2) a literal real number (3.3.3) a literal string (3.3.5) a function designator (7.1.3)

NIL (5.3.3) ( expression) NOT factor [element [) expression[ .

element]... ] . expression]

where element is given by

The last item in the list of forms for factor represents a set, and each element stands for one or more set members. If two expressions joined by ellipses are given, the element represents all the elements within the given subrange of the base type of the set.

Pascal-86 User's Guide Expressions and Statements

The following are some examples of factors, terms, simple expressions, and expressions.

Factors:

x 1 5

(x+y+z) abs(x+y) [red,c,green]

[1,5,10 . . 19,23]

NOT P

Terms:

x*y i / ( 1 - i ) P AND q

(x ( = y) AND (y ) z)

Simple expressions:

x+y - x

hue1 + hue2

i

*

j + 1

Expressions:

x 1 • 5

P < = q

( i < j ) ( j ) k)

c IN hue1

Note that all the examples of factors, terms, and simple expressions are also expressions.

7.1.2

Operands

As defined in the syntax for an expression, an operand may be an entire variable, structured variable component, referenced variable, constant, literal value (integer, real number, or string), the pointer value NIL, or a sequence of set elements enclosed in brackets. It may also be a function designator, which itself directs an operation to be performed.

Note that some operations are defined only for certain operand types or forms. Also note that certain type conversions occur automatically when expressions are evalu-ated at run time.

Im Dokument PASCAL-a6 USER'S GUIDE (Seite 88-91)