Statements

If-Else Statement

The if-else statement offers support for conditionally executing operations.

Syntax

The general syntax for this is:

1 2 3 4 5 6 7 8 9 if(condition) { Instruction1; ... InstructionN; } else { Instruction1: ... InstructionN; }

 

Note: the else branch can be omitted

Example

1 2 3 4 5 6 7 8 9 10 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:

1 2 3 4 5 for(<init>; <condition>; <increment>){ Instruction1; ... InstructionN; }

 

For this first form, the <init> can be an attribution of an already defined variable or a definition for a new variable.

The For Each Form

The for each form can be expressed as follows:

1 2 3 4 5 for(<variable_definition> in #{array}){ Instruction1; ... InstructionN; }

Examples 

Example 1 (standard form)

Prints the numbers from 0 to 9*

1 2 3 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*

1 2 3 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

1 2 3 4 5 while(condition) { Instruction1; ... InstructionN; }

Example

1 2 3 4 5 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 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:

1 2 3 4 5 do { Instruction1; ... InstructionN; } while(condition);

Example

1 2 3 4 5 6 number i = 1; string [] people; do { people += watchers[i]; i = i + 1; } while(i < 5);

Switch Statement

The switch statement offers support for executing different operations depending on the value of a variable. The variable can be either a number or a string.

Syntax

The general syntax:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 switch(variable) { case value1: Instruction11; ... Instruction1N; [break;] case value2: Instruction21; ... Instruction2N; [break;] ... case valueN: InstructionM1; ... InstructionMN; [break;] [default: InstructionDef1; ... InstructionDefN;] }

 

Note: the default branch can be omitted as well as the break statements.

Example

1 2 3 4 5 6 7 8 9 10 switch (season) { case "Summer": print("It's too hot!"); break; case "Winter": print("It's too cold!"); break; default: print("It's just fine!"); }

Break Statement

The break statement can be used in the following situations:

  • inside a loop (for, while): when encountering the break, the execution of the loop is terminated and the control is transferred to the statement that follows the loop;

  • in switch statements: when a case is followed by a break, it does not execute subsequent cases and control is transferred out of the switch statement.

Example

1 2 3 4 5 6 for (number i = 0; i < 10; i = i + 1) { if (i >= 5) { break; } print(i); }

Continue Statement

The continue statement is used inside a loop(for, while) causing it to jump to the next iteration, skipping the execution of the rest of the statements inside the loop for the current iteration.

Example

1 2 3 4 5 6 7 8 9 number i = 1; while (i <= 10) { if (i % 3 == 0) { i = i + 1; continue; } print(i); i = i + 1; }

See also