We have already encountered examples of both statements and expressions. So far we have seen simple statements such as subroutine calls like Writeln('Hello World!'). We have also seen compound statements in the form of begin-end blocks. In the rest of this chapter we will encounter structured statements such as loops and conditionals.
Expressions are combinations of variables, constants, operators, and functions that are evaluated according to the order of operations. Expressions are generally identical to what one would expect from basic mathematics with a few slight variations on the apperance of operators. For example in Object Pascal, addition and subtraction are + and - while multiplication and division are * and /. These operators are known as arithmetic operators and they combine with numbers (known as operands) to form arithmetic expressions. There are 6 arithmetic operators in Object Pascal: +, -, *, /, div, and mod. div and mod are special integer division operators that return the whole part of the quotient and the remainder respectively. Thus, 5 div 3 is 1 while 5 mod 3 is 2 (since 5 is divisible by 3 once with a remainder of 2). Parenthesis ( ) are also available, but Object Pascal does not have a native exponent symbol. The six arithmetic operators are known as binary operators because they operate on 2 operands at a time. If + or - are used with a number by themselves such as -3 then they act as unary operators because they effect only one operand.
Strings can also be combined into expressions. The most common string expression is called concatenation in which multiple strings are joined: 'test'+' string' is equivalent to 'test string'.
Excercise 2-5. Evaluate each of the following expressions.
Using the proper order of operations this expression evaluates to 58.
"mod" and "div" obey the same operator precedence as division. Thus, you should evaluate 4*3 = 12 and then 12 mod 2 = 0 before adding 14 div 4 = 3. The final answer is 3.
This expression will not compile in Object Pascal (although it will in some languages evaluate to 'some text2') We will explain more later on why Object Pascal prevents the mixing of arithmetic and string expressions.
This expression evaluates to 'some text'. The '' on each end is called a null string which basically means it contains nothing and is eliminated in the concatenation.
This expression evaluates to -3. All four operators are treated as unary operators and are equivalent to (-1)*(-1)*(1)*(-1)*3
Boolean expressions are a third type of expression that are equally as important as arithmetic and string expressions. Boolean expressions can have only 1 of 2 values: True or False. Boolean expressions will be used later in Unit 1 to control the flow of instruction execution. The Boolean operators in Object Pascal are fairly intuitive: not, and, or, xor. not negates the truth value of the expression. So not True evaluates to False. and requires that both operators be True in order to evaluate to True, otherwise it returns False. So True and False evaluates to False while True and True evaluates to "True". or will evaluate to True if either operator is True. Thus, unlike and, True or False evaluates to True. xor evaluates to True if and only if one value is True and the other is False. Thus, True xor False evaluates to True while True xor True evaluates to False. The Boolean operators obey the same operator precedence as addition and subtraction.
Excercise 2-6. Evaluate each of the following Boolean expressions.
True and False and True xor True
Boolean operators obey a precedence like addition and subtraction so you simply evaluate the expression left to right:
True and False and True xor True
False and True xor True
False xor True
True
not ((True or False) and (False xor False)
not ((True or False) and (False xor False)
not (True and False)
not False
True
The fourth type of expression is the relational expression. This is where arithmetic and string expressions meet Boolean expressions. Relational expressions always evaluate to True or False but they take numbers or strings as operands. For example, the expression 5 = 3 is False while the expression 5 > 3 is true. There are 6 relational operators in Object Pascal: equal =, not equal <>, less than <, greater than >, less than or equal <=, and greater than or equal >=. Both string and number operands are allowed but operands used simultaneously must be logically compatible: 'test' > 3 will not compile but 2.3 > 3 will. All relational operators have the same precedence as an equal sign in mathematics (i.e., the highest) which means that they are evaluated last.
Excercise 2-7. Evaluate each of the following expressions.
(3 > 2) or (2+1<>4) <> not (2*5 > -1)
(3 > 2) or (2+1<>4) <> not (2*5 > -1)
True or True <> not True
True <> False
True
String operands are compared just as if you were placing strings in alphabetical order. 'test' would precede 'test3' in an alphabetized list just like 3 would precede 4. Thus 'test' < 'test3' is True.
One of the key differences between statements and expressions in Object Pascal is that statements can stand alone in a begin-end block while expressions must be contained inside of statements. In other words, the following code will not compile:
program Workspace;
begin
-11.5+1.5
end.
The statements inside of a begin-end compound statement are said to compose a statement list. As you may have already noticed, in Object Pascal, statements in a statement list must always be separated by semicolons. Leaving off a semicolon from the end of a line is one of the most common mistakes in coding and can occasionally lead to strange error messages from the compiler. Thus, it is always something to check for if your code will not compile.