A **Pascal** **expression** is a collection of functions, constants, variables, and operators that results in a single value. They help in carrying out calculations or comparisons to arrive at values or manage the behavior of the program.

Expressions are made of two components: **operators** i.e., +, -, and their **operands** i.e., A, B. Most of the operators are binary operators they come up between the operands (as in A+B). There are a few unary operators that use only one operand. They are always placed before the operand i.e., -A.

We define different types of operators in Pascal along with their precedence in the following table:

Operator | Precedence | Category |

| Highest (first) | Unary operators, power |

*, /, | Second | Multiplying operators |

+, -, | Third | Adding operators |

=, <>, <, >, <=, >=, | Lowest (last) | Relational operators |

The syntax of an expression in Pascal is as follows:

<expression> ::= <simple expression> | <simple expression> <relational operator> <simple expression><simple expression> ::= [<sign>] <term> {<adding operator> <term>}

Syntax for a Pascal expression

There are different components (operators and operands), constants, variables, and function calls used within an expression. They follow the particular syntax:

<term> ::= <factor> {<multiplying operator> <factor>}<factor> ::= <variable> | <constant> | (<expression>) | <function call><variable> ::= <identifier><constant> ::= <number> | <character> | <string><function call> ::= <identifier> [(<argument list>)]<argument list> ::= <expression> {,<expression>}<relational operator> ::= = | <> | < | > | <= | >=<adding operator> ::= + | - | or<multiplying operator> ::= * | / | div | mod | and

Syntax for components used within an expression

Some examples of expressions in Pascal are:

`x + y`

`3 * (x - 2) / 4`

`a <> b`

(`a`

is not equal to`b`

)`sqrt(x) + power(y, 2)`

The compiler applies the following rules to establish precedence:

Operation operands go to the operator with the highest precedence in operations with unequal precedence. For instance, in

$3 \times 3 + 7$ , the multiplication operator has higher precedence than the addition, so we evaluate it first. The result is 16.If we use parentheses in an expression, then we will evaluate the contents in parentheses in an expression first. For example, the result of

$5\times (3 + 7)$ is 50.The binary operators of the same precedence are left-associative. Thus, on evaluating

$5\times 3 / 7$ , the result is equal to 2.

Note:The order in which expressions use operators of the same precedence is not guaranteed to be left-to-right.

Let's implement these precedence rules by running them on our compiler:

varresult1: Integer;result2: Integer;result3: Integer;beginresult1 := 3 * 3 + 7;writeln('Result for first expression: ', result1);result2 := 5 * (3 + 7);writeln('Result for second expression: ', result2);result3 := 5 * 3 div 7;writeln('Result for third expression: ', result3);end.

In the output above, we can see the results of the first, second and third expressions.

Hence, expressions are used extensively in mathematical computations and in carrying out necessary operations with respect to operator precedence to accomplish desired program behavior.

Copyright ©2024 Educative, Inc. All rights reserved

TRENDING TOPICS