If-Else Statements in Scripts

If-Else statement blocks are used to test conditions that control branching of statement execution within a script. Else-if and Else instructions within the statement block provide a significant amount of control and flexibility.

 An If-Else statement block can be inserted by selecting “IF-ELSE Statement” from the Script element’s Insert menu (or pressing Ctrl+I when in the Script dialog). The block is inserted below the statement that was selected when this is done.  The following dialog will be displayed:

The condition defaults to False.  You must enter a statement which evaluates to True or False (e.g., “Etime < 10 day”).  After doing so, the script will look like this:

Obviously, such a statement block serves no function unless it contains some statements inside the block.  To insert a statement inside the block, you would select the first part of the If-Else block (in this case, line 1), and insert a statement.  In the example below, a Variable Assignment statement has been inserted:

In this case, when the Script element was updated, if Etime was less than 10 days, Result would be set to A + 1 (A is an output external to the Script element). 

You can also move existing statements that are above or below the If-Else block into the block by using the buttons at the bottom of the Script dialog.

   Note: If multiple statements are selected (by left-clicking and dragging the line numbers) when the If-Else statement is first inserted (and the selection is a logically complete block of code), GoldSim will “wrap” the selection in the initial If statement. 

In many cases, an If-Else block will have multiple branches.  The simplest such case is one in which the original If condition results in one branch if it is True, and a separate branch if it is False.  This can be implemented by checking the Provide else clause box in the If-Else dialog.  In the example below, an else clause (and a corresponding Variable Assignment) has been added:

(The Variable Assignment statement could be added to the else clause by selecting the ELSE statement (line 3 in this case) and inserting the new statement, or moving it from elsewhere in the script.)

If you need more complex branching, up to four else if clauses can be inserted by clicking the Click to insert “else if” clause hyperlink on the If-Else dialog.  In the example below, two clauses are inserted:

After inserting corresponding Variable Assignments for each clause, the script would look like this:

   Note: Within an If-Else statement block, if and else if conditions are evaluated sequentially.  As soon as a True statement is found, the statements corresponding to that clause are implemented, and the remaining clauses are skipped.  Hence, if two clauses have conditions that evaluate to True, only the statements associated with the first one are implemented.

One key property of If-Else statement blocks that must be understood is that each clause creates local scopes for any variables that are defined within them.  For example, consider the following script:

In this example, the local script variable Var1 is created in the first clause. This variable is local to that clause.  That is, it cannot be referenced outside of that clause (without redefining it).  If you wanted to reference it throughout the entire If-Else block, you would need to define it prior to the If-Else block.

Related Topics…

Learn more about: