Pascal Programming/Expressions and Branches
In this chapter you will learn
- to distinguish between statements and expressions, and
- how to program branches.
Statement
editBefore we get know “expressions”, let’s define “statements” more precisely, shall we: A statement tells the computer to change something. All statements in some way or other change the program state. Program state refers to a whole conglomerate of individual states, including but not limited to:
- the values variables have, or
- in general the program’s designated memory contents, but also
- (implicitly) which statement is currently processed.
The last metric is stored in an invisible variable, the program counter. The PC always points to the currently processed statement. Imagine pointing with your finger to one source code line (or, more precisely, statement): “Here we are!” After a statement has successfully been executed, the PC advances to the effect that it points to the next statement.[fn 1] The PC cannot be altered directly, but only implicitly. In this chapter we will learn how.
Classification
editStatements can be categorized into two groups: Elementary and complex statements. Elementary statements are the minimal building blocks of high-level programming languages. In Pascal they are:[fn 2][fn 3]
- Assignments (
:=
), and - Routine[fn 4] invocations (such as
readLn(x)
andwriteLn('Hi!')
).
“Complex” statements are:
- Sequences (surrounded by
begin
andend
), - branches, and
- loops.
Semicolon
editUnlike many other programming languages, in Pascal the semicolon ;
separates two statements.
Lots of programming languages use some symbol to terminate a statement, e. g. the semicolon.
Pascal, however, recognized that an extra symbol should not be part of a statement in order to make it an actual statement.
The helloWorld
program from the second chapter could be written without a semicolon after the writeLn(…)
, because there is no following statement:
program helloWorld(output);
begin
writeLn('Hello world!')
end.
We, however, recommend you to put a semicolon there anyway, even though it is not required. Later in this chapter you will learn one place you most probably (that means not necessarily always) do not want to put a semicolon.
Although a semicolon does not terminate a statement, the program header, constant definitions, variable declarations and some other language constructs are terminated by this symbol. You cannot omit a semicolon at these locations.
Expressions
editExpressions, in contrast to statements, do not change the program state. They are transient values that can be used as part of statements. Examples of expressions are:
42
,'D’oh!'
, orx
(wherex
is the name of a previously declared variable).
Every expression has a type:
When an expression is evaluated it results in a value of a certain data type.
The expression 42
has the data type integer
, 'D’oh!'
is a “string type” and the expression merely consisting of a variable’s name, such as x
, evaluates to the data type of that variable.
Because the data type of an expression is so important, expressions are named after their type.
The expression true
is a Boolean expression, as is false
.
Using expressions
editExpressions appear at many places:
- In the assignment statement (
:=
) you write an expression on the RHS. This expression has to have the data type of the variable on the LHS.[fn 5] An assignment makes the transient value of an expression “permanent” by storing it into the variable’s memory block. - The parameter lists of routine invocations consist of expressions. In order to invoke a routine all the parameters have to be stored in memory. Think of a routine invocation as a sequence of assignments to invisible variables before the routine is actually called. Thus
writeLn(output, 'Hi!')
can be understood as- destination becomes
output
- “first parameter” becomes
'Hi!'
- call the routine
writeLn
with the invisible “variables” destination and “first parameter”
- destination becomes
- In a constant definition the RHS is also an expression, although – hence their name – it has to be constant. You could not use, for instance, a variable as part of that expression.
Linking expressions
editThe power of expressions lies in their capability to link with other expressions.
This is done by using special symbols called operators.
In the previous chapter we already saw one operator, the equals operator =
.
Now we can break up such an expression:
response = 'y' {
│ └──────┰─────┘ ┃ └──────┰──────┘ │
│ sub-expression operator sub-expression │
│ │
└─────────────────────┰─────────────────────┘
expression }
As you can you can see in the diagram, an expression can be part of a larger expression.
The sub-expressions are linked using the operator symbol =
.
Sub-expressions that are linked via, or associated with an operator symbol are also called operands.
Comparisons
editLinking expressions via an operand “creates” a new expression which has a data type on its own.
While response
and 'y'
in the example above were both char
-expressions, the overall data type of the whole expression is Boolean
, because the linking operator is the equal comparison.
An equal comparison yields a Boolean expression.
Here is a table of relational operators which we can already use with our knowledge:
name | source code symbol |
---|---|
=, equals | =
|
≠, unequal | <>
|
<, less than | <
|
>, greater than | >
|
≤, less than or equal to | <=
|
≥, greater than or equal to | >=
|
Using these symbols yield Boolean expressions.
The value of the expression will be either true
or false
depending on the operator’s definition.
All those relational operators require operands on both sides to be of the same data type.[fn 6]
Although we can say '$' = 1337
is wrong, that means it should evaluate to the value false
, it is nevertheless illegal, because '$'
is a char
‑expression and 1337
is an integer
‑expression.
Pascal forbids you to compare things/objects that differ in their data type.
So, I guess, y’can’t compare apples ’n’ oranges after all.
(Note, a few conversion routines will allow you to do some comparisons that are not allowed directly, but by taking a detour. In the next chapter we will see some of them.)
Calculations
editExpressions are also used for calculations, the machine you are using is not called “computer” for no reason.
In Standard Pascal you can add, subtract, multiply and divide two numbers, i. e. integer
‑ and real
‑expressions and any combination thereof.
The symbols that work for all combinations are:
name | source code symbol |
---|---|
+, plus | +
|
−, minus | -
|
×, times | *
|
The division operation has been omitted as it is tricky, and will be explained in a following chapter.
If at least one of the operands is a real ‑expression, the entire expression is of type real , even if the exact value could be represented by an integer .
|
Note, unlike in mathematics, there is no invisible times assumed between two “operands”:
You always need to write the “times”, meaning the asterisk *
explicitly.
The operator symbols +
and -
can also appear with one number expression only.
It then indicates the positive or negative sign, or – more formally – sign identity or sign inversion respectively.
Operator precedence
editJust like in mathematics, operators have a certain “force” associated with them, in CS we call this operator precedence. You may recall from your primary or secondary education, school or homeschooling, the acronym PEMDAS: It is a mnemonic standing for the initial letters of
- parentheses
- exponents
- multiplication / division
- addition / subtraction
giving us the correct order to evaluate an arithmetic expression in mathematics. Luckily, Pascal’s operator precedence is just the same, although – to be fair – technically not defined by the word “PEMDAS”.[fn 7]
As you might have guessed it, operator precedence can be overridden on a per-expression basis by using parentheses:
In order to evaluate 5 * (x + 7)
, the sub-expression x + 7
is evaluated first and that value is then multiplied by 5
, even though multiplication is generally evaluated prior sums or differences.
Branches
editBranches are complex statements.
Up to this point all programs we wrote were linear:
They started at the top and the computer (ideally) executed them line-by-line until the final end.
.
Branches allow you to choose alternative paths, like at a T‑bone intersection: “Do I turn left or do I turn right?”
The general tendency to process the program “downward” remains, but there is (in principle) a choice.
Conditional statement
editLet’s review the program iceCream
from the previous chapter.
The conditional statement is highlighted:
program iceCream(input, output);
var
response: char;
begin
writeLn('Do you like ice cream?');
writeLn('Type “y” for “yes” (“Yummy!”) and “n” for “no”.');
writeLn('Confirm your selection by hitting Enter.');
readLn(input, response);
if response = 'y' then
begin
writeLn('Awesome!');
end;
end.
Now we can say that response = 'y'
is a Boolean expression.
The words if
and then
are part of the language construct we call conditional statement.
After then
comes a statement, in this case a complex statement:
begin … end
is a sequence and considered to be one statement.
If you remember or can infer from the source code, the statements between begin … end
, the writeLn('Awesome!')
is only executed if the expression response = 'y'
evaluated to true
.
Otherwise, this is skipped as if there was nothing.
Due to this binary nature – yes / no, execute the code or skip it – the expression between if
and then
has to be a Boolean expression.
You cannot write if 1 * 1 then …
, since 1 * 1
is an integer
-expression.
The computer cannot decide based on an integer
-expression, whether it shall take a route or not.
Alternative statement
editLet’s expand the program iceCream
by giving an alternative response if the user says not to like ice cream.
We could do this with another if
‑statement, yet there is a smarter solution for this frequently occurring situation:
if response = 'y' then
begin
writeLn('Awesome!');
end
else
begin
writeLn('That’s a pity!');
end;
The highlighted alternative, the else
‑branch, will only be executed if the supplied Boolean expression evaluated to false
.
In either case, regardless whether the then
‑branch or the else
‑branch was taken, program execution resumes after the else
‑statement (in this after the end;
in the last line).
Relevance
editBranches and (soon explained) loops are the only method of modifying the PC, “your finger” pointing to the currently executed statement, based on data, an expression, and thus a means of responding to user input. Without them, your programs would be static and do the same over and over again, so pretty boring. Utilizing branches and loops will make your program way more responsive to the given input.
Tasks
editchar
-expressions? All? None?'0'
through '9'
, 'A'
through 'Z'
, and 'a'
through 'z'
are sorted as you are familiar from the English alphabet, or – with respect to the digits – their numeral value in ascending order.
Because of that you are allowed to make a comparison such as 'A' <= 'F'
(which will evaluate to true
).
Notes:
- ↑ This paragraph intentionally uses imprecise terminology to keep things simple. The PC is in fact a processor register (e. g.
%eip
, extended instruction pointer) and points to the following instruction (not current statement). See Subject: Assembly languages for more details. - ↑ Jumps (
goto
) have been deliberately banned into the appendix, and are not covered here, yetgoto
is also an elementary statement. - ↑ Exception extensions also define
raise
as an elementary statement. - ↑ More correctly:
procedure
calls. - ↑ Or, a “compatible” data type, e. g. an
integer
expression can be stored into a variable of the data typereal
, but not the other way round. As we progress we will learn more about “compatible” types. - ↑ In the chapter on sets we will expand this statement.
- ↑ To read the technical definition, see § Expressions, subsection 1 “General” in the ISO standard 7185.