Using IF and WHILE Statements in IBM Planning Analytics TurboIntegrator Processes

IBM Planning Analytics provides powerful scripting capabilities within TurboIntegrator (TI) processes, allowing developers to create dynamic, flexible models. Two foundational constructs used in these scripts are the IF and WHILE statements. IF statements are primarily used for conditional execution of code, and WHILE statements are used to execute a portion of code iteratively. This article explores how to effectively use these statements to control logic and iterate through operations.

IF Statements

An IF statement is used to evaluate a condition and execute a block of code only if that condition is true. Optionally, as many ELSEIF statements can be included within the IF statement to check multiple conditions and execute different code respectively. As another optional addition to the statement, an ELSE can be used to execute code when no other conditions in the IF statement have been met.

Syntax

IF( Condition ) ;

# Code to execute if Condition is true;

ELSEIF( AnotherCondition ) ;

# Code to execute if AnotherCondition is true and no preceding conditions are true;

ELSE ;

# Code to execute if no preceding conditions are true;

ENDIF ;

As seen in the syntax above, the IF statement starts with “IF” directly followed by the condition that is being checked by the statement within parentheses. The code in the lines below the statement will execute as long as the condition is true. Any applicable code can be executed here, including additional nested IF statements. The code to be executed is traditionally indented slightly to make the code easier to read.

At this point, if there is only one condition to check, the IF statement can be finished by adding the ENDIF to a new line under the current code. The IF statement, as a whole, includes everything between the “IF” and the “ENDIF”. If there are additional conditions to be checked with associated code to be run, ELSEIF statements can be included. IF there is code that should be run when all checked conditions are false, an ELSE statement can be added.

It should be noted that no mater how many conditions are checked during the statement, only the first condition that is TRUE will execute its associated code. Because of this, the order of conditions to check is important when adding ELSEIF to the statement. If the first condition is evaluated to be TRUE, no other code associated with other conditions will be executed even if their conditions would have evaluated to be TRUE.

Practical Example

IF( x > 5 ) ;

answer = ‘x is greater than 5’ ;

ELSEIF( x < 5 ) ;

answer = ‘x is less than 5’ ;

ELSE ;

answer = ‘x is 5’ ;

ENDIF ;

Simply defines a text variable based on the value of x.

 

Tips for Effective Use

  • Combine Conditions: Use logical operators (AND, OR, NOT) to combine multiple conditions.
  • Nest IF Statements: Use nested IF statements to evaluate complex conditions.
  • Check Execution Order: Only the first true condition will execute its code. Ensure conditions are written in the intended order.
  • Readability: Indent lines of code after their associated conditions to improve the readability of the code. This is especially important when nesting additional statements.

WHILE Statements

A WHILE statement is used to repeatedly execute a block of code while a specified condition is true. It’s ideal for looping through operations.

Syntax

WHILE( Condition ) ;

# Code to execute repeatedly while Condition is true;

IF( BreakCondition ) ;

BREAK;

ENDIF;

END;

As seen in the syntax above, the WHILE statement starts with “WHILE” directly followed by the condition that is being checked by the statement within parentheses. The code in the lines below the statement will execute as long as the condition is true. Once reaching the “END” at the end of the statement, the condition will be evaluated again. If it is still TRUE, the code within the WHILE statement will execute again. If FALSE, the statement is complete and will not execute again. The code to be executed is traditionally indented slightly to make the code easier to read.

Optionally, an IF statement can be included in the loop to stop execution of the WHILE loop with a BREAK statement if the condition is met. This can be helpful to avoid endless loops, or preventing unnecessary iterations.

Practical Example

Count = 1 ;

Max = 5 ;

WHILE( Count <= Max ) ;

ASCIIOutput( ‘count.txt’ , NumberToString( Count ) ) ;

Count = Count + 1 ;

END ;

Simply loops through the numbers 1 to 5 and outputs them to a file

Tips for Effective Use

  • Avoid Infinite Loops: Ensure the condition changes within the loop to prevent endless execution. Often a counting variable is used to help with this. If using a counting variable, ensure it is incremented appropriately within the statement.
  • Use Break Conditions: Combine IF statements to exit loops early if needed.
  • Readability: Indent lines of code after their associated conditions to improve the readability of the code. This is especially important when nesting additional statements.

Common Pitfalls and How to Avoid Them

  • Mismatched Syntax: Ensure each IF has a matching ENDIF and each WHILE has an END. Placing the statements in the correct location in the code is also vitally important to ensuring the logic functions as intended.
  • Unoptimized Conditions: Evaluate conditions carefully to avoid unnecessary processing overhead.
  • Infinite Loops: Always test loops thoroughly to confirm they terminate correctly.
  • Excessive Looping: Looping over large data sets, especially if nesting WHILE loops can cause overhead in a process.