Материал готовится,

пожалуйста, возвращайтесь позднее

пожалуйста, возвращайтесь позднее

When you build more complicated arithmetic expressions, you have to watch the priority

of each operator involved in the expression. Take a look at these two

expressions:

2 + 10 / 2

(2 + 10) / 2

Although the two expressions use the same numbers and operators, the first expression

is evaluated as 7, while the second is evaluated as 6. This is because in the first

expression the division is evaluated before the addition, while in the second expression

the parentheses are used to change the order of evaluation, in which case the

expression inside the parentheses is evaluated first. In general, the arithmetic operators

in Pascal have two levels of precedence: high and low.

The + and – have low precedence, while all other operators have high precedence.

If an expression contains two operators of the same precedence level, they are evaluated

from left to right. Consider this example:

5 + 3 * 2 – 6 DIV 2

The first operation to be performed is the multiplication:

5 + 6 – 6 DIV 2

The second operation, of next highest priority, is the division:

5 + 6 – 3

This leaves two operations of equal priority. They are evaluated from left to right

giving:

8

When parentheses are used to alter the order of evaluation, they form subexpressions

which are evaluated first. If parentheses are nested, the innermost subexpressions are

evaluated first. Consider the same example with nested parentheses:

((5 + 3) * 2 – 6) DIV 2

This expression is evaluated according to the following steps:

(8 * 2 – 6) DIV 2

(16 – 6) DIV 2

10 DIV 2

5

Arithmetic operators are summarized in Table 1-1, along with their precedence and

properties.

The + and – signs are also used as unary operators (to signify positive and negative).

The unary operators are of the same low priority as the binary operators + and –. If a

binary operator precedes the unary operator such as 5 * – 4, you must enclose the

unary operator and its number in parentheses: 5 * (–4). The first form may be

accepted by some compilers, but do not try it.

Table 1-1 Arithmetic operators

Operator Arithmetic Operation Operands Result Precedence

+ Addition REAL/INTEGER REAL/INTEGER Low

– Subtraction REAL/INTEGER REAL/INTEGER Low

* Multiplication REAL/INTEGER REAL/INTEGER High

/ Real division REAL/INTEGER REAL High

DIV Integer division INTEGER INTEGER High

Drill 1-3

Evaluate the following expressions and write the result either as an integer (if integer)

or as a fixed-point real number (if real):

A. 15 – 15 DIV 15

B. 22 + 10 / 2

C. (22 + 10) / 2

D. 50 * 10 – 4 MOD 3 * 5 + 80

of each operator involved in the expression. Take a look at these two

expressions:

2 + 10 / 2

(2 + 10) / 2

Although the two expressions use the same numbers and operators, the first expression

is evaluated as 7, while the second is evaluated as 6. This is because in the first

expression the division is evaluated before the addition, while in the second expression

the parentheses are used to change the order of evaluation, in which case the

expression inside the parentheses is evaluated first. In general, the arithmetic operators

in Pascal have two levels of precedence: high and low.

The + and – have low precedence, while all other operators have high precedence.

If an expression contains two operators of the same precedence level, they are evaluated

from left to right. Consider this example:

5 + 3 * 2 – 6 DIV 2

The first operation to be performed is the multiplication:

5 + 6 – 6 DIV 2

The second operation, of next highest priority, is the division:

5 + 6 – 3

This leaves two operations of equal priority. They are evaluated from left to right

giving:

8

When parentheses are used to alter the order of evaluation, they form subexpressions

which are evaluated first. If parentheses are nested, the innermost subexpressions are

evaluated first. Consider the same example with nested parentheses:

((5 + 3) * 2 – 6) DIV 2

This expression is evaluated according to the following steps:

(8 * 2 – 6) DIV 2

(16 – 6) DIV 2

10 DIV 2

5

Arithmetic operators are summarized in Table 1-1, along with their precedence and

properties.

The + and – signs are also used as unary operators (to signify positive and negative).

The unary operators are of the same low priority as the binary operators + and –. If a

binary operator precedes the unary operator such as 5 * – 4, you must enclose the

unary operator and its number in parentheses: 5 * (–4). The first form may be

accepted by some compilers, but do not try it.

Table 1-1 Arithmetic operators

Operator Arithmetic Operation Operands Result Precedence

+ Addition REAL/INTEGER REAL/INTEGER Low

– Subtraction REAL/INTEGER REAL/INTEGER Low

* Multiplication REAL/INTEGER REAL/INTEGER High

/ Real division REAL/INTEGER REAL High

DIV Integer division INTEGER INTEGER High

Drill 1-3

Evaluate the following expressions and write the result either as an integer (if integer)

or as a fixed-point real number (if real):

A. 15 – 15 DIV 15

B. 22 + 10 / 2

C. (22 + 10) / 2

D. 50 * 10 – 4 MOD 3 * 5 + 80

Загрузка...

Выбрать следующее задание

Ты добавил

Выбрать следующее задание

Ты добавил