Pascal Programming/Expressions and Branches

In this chapter you will learn
 * to distinguish between statements and expressions, and
 * how to program branches.

Statement
Before 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 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. The PC cannot be altered directly, but only implicitly. In this chapter we will learn how.
 * the values variables have, or
 * in general the program’s designated memory contents, but also
 * (implicitly) which statement is currently processed.

Classification
Statements 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: “Complex” statements are:
 * Assignments, and
 * Routine invocations (such as  and  ).
 * Sequences (surrounded by  and  ),
 * branches, and
 * loops.

Semicolon
Unlike many other programming languages, in Pascal the semicolon  separates two statements. Lots of programming languages use some symbol to terminate a statement, e.&#8239;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  program from the second chapter could be written without a semicolon after the , because there is no following statement: 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
Expressions, 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: Every expression has a type: When an expression is evaluated it results in a value of a certain data type. The expression  has the data type ,   is a “string type” and the expression merely consisting of a variable’s name, such as  , 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  is a Boolean expression, as is.
 * , or
 * (where  is the name of a previously declared variable).
 * (where  is the name of a previously declared variable).

Using expressions
Expressions 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. 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  can be understood asdestination becomes  “first parameter” becomes  call the routine   with the invisible “variables” destination and “first parameter”For the first two pseudo-assignments the value/expression on the RHS had to be assignment-compatible with the LHS.
 * 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
The 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: 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
Linking expressions via an operand “creates” a new expression which has a data type on its own. While  and   in the example above were both  -expressions, the overall data type of the whole expression is , 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: Using these symbols yield Boolean expressions. The value of the expression will be either  or   depending on the operator’s definition.

All those relational operators require operands on both sides to be of the same data type. Although we can say  is wrong, that means it should evaluate to the value , it is nevertheless illegal, because   is a  ‑expression and   is an  ‑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
Expressions 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. ‑ and  ‑expressions and any combination thereof. The symbols that work for all combinations are: The division operation has been omitted as it is tricky, and will be explained in a following chapter.

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
Just 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 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”.
 * 1) parentheses
 * 2) exponents
 * 3) multiplication&#8239;/&#8239;division
 * 4) addition&#8239;/&#x202F;subtraction

As you might have guessed it, operator precedence can be overridden on a per-expression basis by using parentheses: In order to evaluate, the sub-expression   is evaluated first and that value is then multiplied by  , even though multiplication is generally evaluated prior sums or differences.

Branches
Branches 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. 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
Let’s review the program  from the previous chapter. The conditional statement is highlighted: Now we can say that  is a Boolean expression. The words  and   are part of the language construct we call conditional statement. After  comes a statement, in this case a complex statement: is a sequence and considered to be one statement.

If you remember or can infer from the source code, the statements between, the   is only executed if the expression   evaluated to. Otherwise, this is skipped as if there was nothing.

Due to this binary nature – yes&#8239;/&#8239;no, execute the code or skip it – the expression between  and   has to be a Boolean expression. You cannot write, since   is an  -expression. The computer cannot decide based on an -expression, whether it shall take a route or not.

Alternative statement
Let’s expand the program  by giving an alternative response if the user says not to like ice cream. We could do this with another ‑statement, yet there is a smarter solution for this frequently occurring situation: The highlighted alternative, the ‑branch, will only be executed if the supplied Boolean expression evaluated to. In either case, regardless whether the ‑branch or the  ‑branch was taken, program execution resumes after the  ‑statement (in this after the   in the last line).

Relevance
Branches 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
Notes: