Info |
---|
This document covers syntax, usage patterns, and practical examples for each statement type used in SIL. |
For Statement
The for statement provides a compact
f to iterate over a range of values.
It is often
referred to
as the for loop statement,because it repeatedly loops until a particular condition is satisfied.
In SIL, the for statement
provides two
primary iteration methods:
standard
form: simply for
foreach form
The
standard form
Syntax
Code Block |
---|
for(<init>; <condition>; <increment>){
Instruction1;
...
InstructionN;
} |
Note |
---|
For |
the standard form, the |
The For Each Form
The for each form can be expressed as follows:Example
Code Block |
---|
// Prints numbers from 0 to 9 for ( |
number |
i = 0; i < 10; i = i + 1) { |
print(i); } |
Examples
Example 1 (standard form)
Prints the numbers from 0 to 9*Example Description
This example demonstrates a basic iteration:
Initializes a counter
i
to 0Continues looping while
i
is less than 10Increments
i
by 1 in each iterationPrints each number from 0 to 9 (total 10 numbers)
The foreach form
Syntax
Code Block |
---|
for( |
<variable_definition> |
in #{array}){ Instruction1; ... InstructionN; } |
Example
Code Block |
---|
for(string user in watchers){
print(user);
} |
Example description
This foreach loop iterates through a collection of watchers of a specific issue and prints the list of the watchers:
Automatically iterates through each element in the
watchers
arrayAssigns each element to the
user
variable in each iterationPrints the name of each watcher
While Statement
The while statement
provides support for
conditional repetative execution of a code block. It evaluates the condition first
:
If the condition is true, the instructions in the body (the loop) is executed.
If the condition is false from the start, the loop body is never executed.
Syntax
Code Block |
---|
while(condition) { Instruction1; ... // Instructions when condition is true InstructionN; } |
Example
Code Block |
---|
number i = 1;
while(i <= 3) {
multisel = arrayAddElement(multisel,"value" + i);
i = i + 1;
} |
Example Description
This while loop in this example demonstrates how to build a collection incrementally:
Starts with
i
as 1Continues looping while
i
is less than or equal to 3In each iteration, adds a new element to
multisel
arrayElement is created by concatenating "value" with the current value of
i
After completion,
multisel
will contain ["value1", "value2", "value"]
Do-While Statement
The do-while statement is similar to the while statement
except that it guarantees at least one execution of its code block before evaluating the termination condition. Even if the condition is false, the instructions will still be evaluated once.
Syntax
The general syntax:
Code Block |
---|
do {
Instruction1;
...
InstructionN;
} while(condition); |
Example
Code Block |
---|
number i = 1;
string [] people;
do {
people += watchers[i];
i = i + 1;
} while(i < 5); |
Example description
This example do-while loop populates a people
array, ensuring at least one processing of the loop body:
Starts with
i
as 1Adds watchers to the
people
array from index 1Increments
i
after each iterationContinues until
i
is no longer less than 5
Switch Statement
The switch statement
provides a structured mechanism for conditional execution based on the value of a specific variable.
It accepts both number and string variables.
Evaluates the input variable against multiple predefined case values.
Executes the first matching case's code block.
Continues executing subsequent cases unless a
break
statement is used.Can support specific handling for unmatched cases through the
default
branch.
Syntax
The general syntax:
Code Block |
---|
switch(variable) {
case value1:
Instruction11;
...
Instruction1N;
[break;]
case value2:
Instruction21;
...
Instruction2N;
[break;]
...
case valueN:
InstructionM1;
...
InstructionMN;
[break;]
[default:
InstructionDef1;
...
InstructionDefN;]
} |
Note |
---|
Note: The break statements and the default branch can be omitted |
, which changes the script behavior. |
Example 1: Switch with numbers (with break statements and default branch)
Code Block |
---|
switch (priority) { // priority is a number variable
case 1:
// Executes only when priority equals 1
escalateImmediately();
break; // Prevents fallthrough to case 2
case 2:
// Executes only when priority equals 2
scheduleReview();
break; // Prevents fallthrough to case 3
case 3:
// Executes only when priority equals 3
addToBacklog();
break; // Prevents fallthrough to default
default:
// Executes when priority doesn't match 1, 2, or 3
logInvalidPriority();
} |
Example 2: Switch with string matching
Code Block |
---|
switch ( |
status) { // status is a string variable case " |
ERROR": |
logError(); // No break - falls through to "WARN" case "WARN": sendAlert(); break; // Stops here for both "ERROR" and "WARN" case " |
INFO": |
logMessage(); break; default: |
// Handles any status value not matching above cases
logUnknownStatus();
} |
Examples description:
In both examples, cases are evaluated from top to bottom. Only equal values trigger case execution.
The break statement terminates the switch block's execution after a case is handled, preventing unintended fallthrough to subsequent cases. The second example demonstrates an intentional fallthrough. For additional information about the break statement, refer to the Break Statement section.
The default branch functions as an essential catch-all mechanism within the switch statement. It executes when the input value doesn't match any of the defined case values, providing a way to handle unexpected or unspecified inputs. While optional, including a default branch is considered best practice as it ensures comprehensive handling of all possible input values and helps prevent undefined behavior.
Break Statement
The break statement
serves two distinct control flow purposes:
Loop termination: When used within loops (for, while
, do-while), the break statement provides immediate termination of the entire loop execution, transferring control to the first statement following the loop block. This enables early exit from loops based on specific conditions.
Switch exit: Within switch statements, break terminates the switch block execution, preventing fallthrough to subsequent cases and transferring control to the code following the switch structure.
Example
Code Block |
---|
// Prints numbers 0-4, then exits loop early for (number i = 0; i < 10; i = i + 1) { if (i >= 5) { |
break; // Terminates loop when i reaches 5 } print(i); // Only prints 0,1,2,3,4 } // Execution continues here after break |
Continue Statement
The
continue statement is used inside a loop (for, while)
. It provides a mechanism to skip the remaining code within the current loop iteration, immediately jumping to the loop’s next iteration.
Example
Code Block |
---|
number i = 1; while (i <= 10) { |
if (i % 3 == 0) { |
i = i + 1; |
// Update counter before continue continue; // Skip printing multiples of 3 } print(i); |
// Prints: 1,2,4,5,7,8,10 i = i + 1; } |
Best practices for using statements in SIL
Following some simple best practices can prevent common pitfalls and ensure your code is maintainable, reliable, and efficient.
General
Keep statement blocks concise and focused.
Avoid mixing too many different control structures.
Use meaningful variable names that indicate purpose.
Add comments for complex logic or business rules.
Test thoroughly.
Include appropriate error-handling mechanisms. For example, validate inputs before processing, log or report errors.
Keep your code well organized by grouping related control structures.
Maintain consistent indentation and formatting
Document complex control flow patterns.
If-Else Statements
Keep conditions simple and readable.
For multiple conditions, consider using switch statements instead.
Avoid deeply nested if statements (maximum 3-4 levels).
Consider extracting complex conditions into separate boolean functions.
Switch Statements
Always include a default case for error handling.
Use break statements consistently to prevent unintended fallthrough.
Document intentional fallthrough cases with comments.
Consider switch instead of if-else when comparing a single variable against multiple values.
For Loops
When the iteration count is known, use the standard form for loops.
When processing arrays, preferably use for each loops.
Initialize variables properly before the loop.
Avoid modifying loop variables inside the loop body.
While Loops
Ensure loop conditions can eventually become false.
Initialize all variables before the loop.
Use while when iteration count is unknown.
Include safeguards against infinite loops.
Do-While Loops
Use when code must execute at least once. Best suited for validation scenarios.
Keep the loop body focused on a single task.
Ensure the condition can be met within the loop body.
Break Statements
Use to exit loops early when a condition is met.
Avoid using multiple breaks in a single loop.
Document the exit condition clearly.
Consider extracting complex logic into separate functions.
Continue Statement
Use to skip iterations based on specific conditions.
Place continue statements early in the loop body.
Avoid multiple continue statements in a single loop.
Ensure all necessary variables are updated before continue.
See also
Contents
toc