BayES BayES

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:

  1. if-else blocks where a block of statements are executed if a condition is true

  2. for and while loops where a block of statements are executed repeatedly for a given number of times or while a condition is true

  3. 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:

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.






Truth value of Truth value of Logical AND Logical OR Logical NOT
A B A & B A | B A





TRUE TRUE TRUE TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
FALSE TRUE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE TRUE





Table 2.1: Logical operators and their syntax in BayES

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:

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:

if ( <boolean expression> ) 
    <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:

if ( <boolean expression> ) 
    <list of statements> 
end

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.

Example 2.7
▼ Input ▼ Output
 
// Define x as a scalar equal to 3 
x = 3; 
 
// Print a message to the console 
// conditional on the value of x 
if (x==3) 
    print("x is equal to 3"); 
else 
    print("x is equal not to 3"); 
end 

 
 x is equal to 3 
 
 
 
 
 
 
 
 
 

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:

for ( <loop index> = <lower value>:<upper value> ) 
    <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
 
// Print the value of the loop 
// index, i, within the loop 
for (i=1:3) 
    print(i); 
end 
 
 
 
 

 
i = 
  1 
 
 
i = 
  2 
 
 
i = 
  3

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:

while ( <boolean expression> ) 
    <list of statements> 
    // these will be executed repeatedly 
end

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.

Example 2.9
▼ Input ▼ Output
 
// Print the value of i within a while 
// loop 
i = 4; 
while (i>0) 
    print(i); 
    i = i - 1; 
end 
 
// It is important to change the value 
// of i inside the loop, otherwise the 
// program will never leave it 
 
 

 
i = 
  4 
 
 
i = 
  3 
 
 
i = 
  2 
 
 
i = 
  1

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.

Share this content:
Facebook Twitter LinkedIn Email
© 2016–20 Grigorios Emvalomatis