2.9 Program flow
Until now this document covered code where the program control flows sequentially through the statements in a script file: statements are interpreted and executed one-by-one in the order they are defined in the script and the program ends when the last statement finishes executing. Oftentimes in practice it is necessary to execute a statement repeatedly or if a condition is satisfied. In these cases program control needs to jump from one point to another and execute statements in a non-linear fashion.
BayES provides three ways of controlling the program flow:
-
if-else blocks where a block of statements are executed if a condition is true
-
for and while loops where a block of statements are executed repeatedly for a given number of times or while a condition is true
-
user-defined functions
User-defined functions are covered in detail in section 2.10. The rest of this section covers conditional execution and loops, after first defining boolean operators.
2.9.1 Boolean expressions and operators
A boolean expression is a logical statement that could be either true or false. For example:
- 1<3 is true
- 1>=3 is false
- x==3 is true or false, depending on whether x was defined to be equal to 3 or not5
Single boolean expressions, like the ones mentioned above, can be combined to form larger expressions using boolean operators. Boolean operators are nothing more than symbols that are used to convey the meaning of truth functionals in formal logic. Suppose that we have two boolean expressions A and B. Table 2.1 shows how these two expressions can be combined using boolean operators and the truth values of the resulting composite expression, depending on the truth values of the two original expressions.
Logical operators in BayES are evaluated from left to right, but parentheses can be used to group composite boolean expressions and alter the order of evaluation. For example if A is true and B false:
- A & B evaluates to false
- (A & B) evaluates to true
2.9.2 if-else statements
if-else statements are used to execute a block of code conditional on the truth value of a boolean expression. An if-else statement has the following general form:
<list of statements>
// these will be executed only if the
// boolean expression evaluates to true
else
<list of statements>
// these will be executed only if the
// boolean expression evaluates to false
end
Note that if there is nothing to be executed if the boolean expression evaluates to false, then the else part can be omitted altogether:
Example 2.7 demonstrates the use of the if-else statement. Sample "6Program flow.bsf", located at "$BayESHOME/Samples/1GettingStarted" contains some more complete examples of the statement.
2.9.3 for loops
for loops are used to repeatedly execute a block of statements for a given number of times. A for statement has the following general form:
<list of statements>
// these will be executed repeatedly
end
In this statement the loop index is an integer which, upon execution, will be set equal to the lower value specified in the first line of the statement. During execution the list of statements will be executed and the loop index value increased by one, until it reaches the upper value specified in the first line of the statement.
Example 2.8 demonstrates the use of the for statement. Sample "6Program flow.bsf", located at "$BayESHOME/Samples/1GettingStarted" contains some more complete examples of the statement.
■ Example 2.8 | |
---|---|
▼ Input | ▼ Output |
|
|
2.9.4 while loops
while loops are used to repeatedly execute a block of statements while a condition is true. A while statement has the following general form:
During execution the boolean expression will be evaluated and, if the result is true, the list of statements inside the block will be executed repeatedly. The block of statements should make changes to items appearing in the boolean expression in every iteration, otherwise the expression will never evaluate to false and the program control will never jump out of the loop.
Example 2.9 demonstrates the use of the while statement. Sample "6Program flow.bsf", located at "$BayESHOME/Samples/1GettingStarted" contains some more complete examples of the statement.
5Notice that when testing for equality between two values, a double equality, ‘==’, is used. This is because the single equality, ‘=’, is reserved for assignments and a statement like x=3 would assign the value 3 to x, instead of testing whether x is equal to 3.