This section contains descriptions of the commands that control the flow of a script. Such commands provide functionality for conditions (if, while) and loops (for).
Note: the information in this section directly references the QT Script for Applications Manual.
Break
This keyword is used in for loops, do loops, while loops and switch statements. When a break statement is encountered in a loop, control is passed to the statement following the end of the inner-most loop that contains the break statement; unless the break statement is followed by the name of a label, in which case control passes to the statement governed by the label.
A break statement is usually placed at the end of each case in a switch statement to prevent the interpreter "falling through" to the next case. When the interpreter encounters a break statement, it passes control to the statement that follows the innermost enclosing switch statement. If every case has a corresponding break, then at most one case's statements will be executed. If the break statement is followed by a label name (label) then when the break is encountered, control will pass to the statement marked with that label; this is useful, for example, for breaking out of deeply nested loops.
Example:
Case
This keyword is used in switch statements. For each possible value that a switch statement's expression may evaluate to, one case may be written (but see default.) If a case's literal value (Value) matches the value of the switch statement's expression, then that case's statements (Statements) are executed.
Normally a case's statements are terminated by a break statement which causes execution to pass to the end of the switch statement.
Continue
This keyword is used within the context of a for, while, or do loop.
If a continue statement is encountered in a for loop, execution immediately passes to the third part of the for loop (normally where a counter is incremented or decremented), and then execution continues normally, i.e. the middle part of the for loop (the conditional) is tested, and if true, the body of the loop is executed.
If a continue statement is encountered in a while or do loop, execution immediately passes to the conditional, which is retested; the body of the loop will be executed if the conditional is still true.
Default
This keyword is used in switch statements. It is used instead of case to match anything to which that the switch statement's expression has evaluated . If no default is used, and none of the cases match, then the switch statement will not execute anything and control will pass on to the following statement. If default is used, it must be the last case in the switch statement. This is because each case is evaluated in order, and since default matches any value, it will always be executed if the interpreter reaches it, and any following cases would always be ignored. When the default case is encountered its DefaultStatements are executed. It is customary to end a default statement with a break.
Do
This keyword is used in conjunction with while to form a loop which is guaranteed to execute at least once.
The Statements in the braces following the do are executed once. If the while condition evaluates to true, execution passes back to the do, and the whole process repeats. If the while loop's conditional ever becomes false, execution continues from the statement following the while statement.
Example:
The example prints 5, 10, 15, ..., 45, 55, 60, 65, ..., 95 on the console.
Else
The else keyword is used in conjunction with if.
For
This keyword is used to create a loop that executes a fixed number of times.
The for statement is broken into the following parts: the keyword for, an opening parentheses, zero or more statements (the first part), a semi-colon, a conditional expression (the second part), a semi-colon, zero or more statements (the third part), a closing parentheses, and finally a statement or block that is governed by the for loop.
The first part of the for statement is typically used to initialize (and possibly declare) the variable used in the conditional in the second part of the for loop statement. This part is executed once before the loop begins. This part may be empty.
The second part contains a conditional expression. The expression is evaluated before each iteration of the loop (including before the first iteration). If this expression evaluates to false, the statement or block governed by the for loop is not executed and control passes to the statement that follows. If the condition is never true, the statement or block governed by the for loop will never be executed. If the condition expression is true, the statement or block governed by the for loop is executed, and then the third part of the for statement is executed, before control is passed back to the conditional expression with the whole process being repeated. This part should not be empty.
The third part contains statements which must be executed at the end of every iteration of the loop. This part is typically used to increment a variable that was initialized in the first part, and whose value is tested in the second part. This part may be empty.
In the following example, entities 44 through 52 are selected.
If
An if statement provides a two-way branch. A multi-way branch is achieved using else ifs.
Switch
A switch statement provides a multi-way branch. The expression is evaluated once, then each case is checked in order to find one with a Value that matches the value of the expression. If a match is made, the Statements of the matching case are executed, after which control passes to the statement following the switch block. If no matching case is found, and there is a default case, the DefaultStatements are executed, after which control passes to the statement following the switch block. If there is no matching case and no default, no statements within the switch block are executed, and control passes to the statement following the switch block.
Note that if a default is used it must come after all the cases; this is because when default is encountered it is treated as a matching case regardless of what follows.
Every case, and the default (if used), should have a break as the last statement. If break is not present, control will "drop through" to the following statements, which is not usually the desired behavior.
The expression may be any arbitrary Autodesk Simulation CFD Script expression that evaluates to an object that can be strictly compared. For example, an expression that evaluates to a Boolean, Date, Number or String value.
While
This keyword is used to repeat a block of code zero or more times. When the while statement is encountered the condition is evaluated. If the condition is true, the Statements in the while block are executed; otherwise control passes to the statement following the while block. If the condition is true, after the Statements have been executed, the condition is again evaluated, and the whole process repeats.
Example: