Contents
Statements provide a convenient method to execute conditional or repetitive operations. The syntax is C/C++/Java like, so things should be easy to grasp.
If-Else Statement
The if-else statement offers support for conditionally executing operations.
Syntax
The general syntax for this is:
if(condition) { Instruction1; ... InstructionN; } else { Instruction1: ... InstructionN; }
Note: the else branch can be omitted
Example
if (isNotNull(fixVersions) and affectedVersions == {"1.1"}) { affectedVersions = {"1.1", "1.0" , "1.2"}; fixVersions = {"1.2" , "1.2" , "1.3"} ; } else { affectedVersions = {"1.1"}; fixVersions = {"1.0"}; }
For Statement
The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the for loop because it repeatedly loops until a particular condition is satisfied.
In SIL the for statement has two forms:
- the standard for or simply for form
- the foreach form
The standard form
Similar to C, C++ or Java of the for statement can be expressed as follows:
for(<init>; <condition>; <increment>){ Instruction1; ... InstructionN; }
For this first form, <init> can be an attribution of an already defined variable or a definition for a new variable.
The foreach form
The foreach form can be expressed as follows:
for(<variable_definition> in #{array}){ Instruction1; ... InstructionN; }
Examples
Example 1 (standard form)
Prints the numbers from 0 to 9*
for(number i = 0; i < 10; i = i + 1){ print(i); }
Example 2 (foreach form)
Prints the list of the watchers of a specific issue*
for(string user in watchers){ print(user); }
While Statement
The while statement offers support for repeated executions. This form evaluates the condition first and then executes the instructions in the body.
Syntax
while(condition) { Instruction1; ... InstructionN; }
Example
number i = 1; while(i <= 3) { multisel = arrayAddElement(multisel,"value" + i); i = i + 1; }
Do-While Statement
The do-while statement is similar to the while statement, except that the condition is evaluated after the execution of the encapsulated block. So,even if the condition is false, the instructions will still be evaluated once.
Syntax
The general syntax is:
do { Instruction1; ... InstructionN; } while(condition);
Example
number i = 1; string [] people; do { people += watchers[i]; i = i + 1; } while(i < 5);