Program Control Structures

Control structures effect the flow of simulation code. Though many of these structures function the same as their counterparts in C, the syntax is slightly different in each case. The main difference is that the statement being tested is not (necessarily) surrounded by parenthesis, but is followed by a colon (':') character.

Control statements evaluate test statements and then execute code according to the result. In the case of for (the section called “The for Loop”), foreach (the section called “The foreach Loop”) and while (the section called “The for Loop”), these structures are used as loops to repeat execution of a piece of code a certain number of times or while a certain condition is met. The if statement is used to execute a block of code if a certain condition is true and, optionally, a different piece of code if the statement is false. A call to return will exit any control structure's block immediately and exit the method.

The conditional statements are be comprised of C-style comparison operators. The following comparison operators are available:

For all of these structures, the code to be executed may be either a single statement, or several statements enclosed in braces ('{' and '}').

The if Statement

The if statement is used to execute one piece of code if a test statement is true, and (optionally) another if the statement is false:

if test_statement: true_code
[ else false_code ]

Examples of the if statement are shown below.

# here we execute a single statement

if x > 5: x = 20.
else x = 0.

# here we execute many...

if x > 5: {
        x = 20.
        y = 40.
} 

# here we execute many in the if, but only one in the else...

if x > 5: {
        x = 20.
        y = 40.
} else x = 200.

The while Loop

The while structure works just like the while statement in C. If executes a block of code repeatedly, as long as the condition statement is true:

while condition: code

Examples of the while loop are shown below.

# for example...

while x < 10: {
        print "x = $x".
        x++.
}

The foreach Loop

The foreach structure is similar to the foreach loop in Perl. The loop iterates through a list, and executes the associated code each time. The current item in the list is stored in a temporary variable as supplied by the user:

foreach temporary_variable in list_variable: code.

Examples of the foreach loop are shown below.

# so, for example, if we have a variable called agent and a list
# of objects stored in agentList:

foreach agent in agentList: {
        print (agent get-location).
}

The for Loop

The for loop (similar to the for loop in C) repeatedly executes a block of code. Though it can function more generally like the while loop, it is typically used to run a block of code for each value of a "counter" variable.

The loop is separated into three statements—an initializer, a test statement, and an increment statement.

for expression, test_expression, increment_expression: code.

The initializer is executed once when the loop starts. It is typically used to set the iteration variable before proceeding. The test statement is run at every iteration to determine whether the loop will continue to execute (similar to the while loop). Finally, the increment statement is run at every iteration of the loop, typically to update a counter variable. Examples of the for loop are shown below.

# so, for example, if we have a variable called n (int), this loop will
# print the numbers from 1 to 30.

for n=0, n<30, n+=1: {
        print n.
}

# we can also use a different increment statement in order to run the 
# loop a bit differently—let's print only even numbers between 1 and 30

for n=2, n<30, n+=2: {
        print n.
}