/
How to work with statements

How to work with statements

Statements are the executable instructions used by a script to evaluate conditions, make decisions, and repeat actions automatically.

Do-while

Do-while loops are similar to while loops but with an important difference: the condition is evaluated at the end of each iteration rather than at the beginning. This means the loop body will always execute at least once, even if the condition is initially false.

Example 1: Guaranteed execution loop
number count = 1; do{ runnerLog(count); count ++; }while(count <= 10);

Use this approach when you need to ensure the loop body executes at least once before checking the condition or when the condition depends on operations performed within the loop.


For

This versatile statement is commonly used when processing collections of data, such as multi-value fields or groups of issues, allowing efficient iteration through each element.

Example 1: Counting loops

for(int x = 1; x <= 10; x++) { runnerLog(x); }

Use this pattern when you need to execute code a specific number of times or when you need a numeric counter that increments with each iteration.

Example 2: Collection-based loops

string[] fruit = "Apple|Apricot|Avocado|Banana|Blackberry|Blueberry|Cherry|Coconut"; for(string f in fruit) { runnerLog(f); }

Use this simplified syntax when you need to process each element in an array or collection without needing to track position information.

Example 3: Index-based array iteration

fruit = "Cranberry|Grape|Grapefruit|Kiwifruit|Lemon|Lime|Mango|Orange"; for(int x = 0; x < size(fruit); x++) { runnerLog("Item #" + (x+1) + ": " + fruit[x]); }

Use this approach when you need to access both the element value and its position within the array, or when you need to manipulate the iteration sequence beyond simple progression.


If-else

If-else statements are foundational building blocks in scripting. They evaluate conditions and execute specific code blocks only when those conditions are true. If a condition is false, the script skips that block and moves to the next condition in the sequence until it finds a true condition or exhausts all options.

Example 1: Simple condition

number monthNumber = month(currentDate()); string monthName = formatDate(currentDate(), "MMMM"); if(monthNumber > 0) { runnerLog("Example 1: It is the month of " + monthName + "!"); }

Use this pattern for straightforward conditions where you only need to perform actions when a single condition is true.

Example 2: Unreachable conditions

number monthNumber = month(currentDate()); string monthName = formatDate(currentDate(), "MMMM"); if(monthNumber > 12) { runnerLog("Example 2: Time no longer exists :("); }

While this example intentionally demonstrates a condition that's always false, in practice, avoid writing code with unreachable conditions as it adds unnecessary complexity.

Example 3: Specific boolean evaluation

number monthNumber = month(currentDate()); string monthName = formatDate(currentDate(), "MMMM"); if(startsWith(monthName, "J") == true) { runnerLog("Example 3: This month " + monthName + " starts with the letter 'J'."); }

Use this approach when you need to check if a specific property or condition is true, especially when working with strings or complex data types.

Example 4: Multiple exclusive conditions

number monthNumber = month(currentDate()); string monthName = formatDate(currentDate(), "MMMM"); if(monthNumber == 1) { runnerLog("Example 4: There are 31 days this month."); } else if(monthNumber == 2) { runnerLog("Example 4: February is such a weird month...."); }

Use if-else-if structures when you need to test multiple conditions in sequence, where only one block should execute even if multiple conditions could be true.

Example 5: Complete conditional flow

number monthNumber = month(currentDate()); string monthName = formatDate(currentDate(), "MMMM"); number[] longMonths = {1,3,5,7,8,10,12}; if(arrayElementExists(longMonths, monthNumber)) { runnerLog("Example 5: There are 31 days this month."); } else if(monthNumber == 2) { runnerLog("Example 5: There are 28 or 29 days this month. Who knows..."); } else { runnerLog("Example 5: There are 30 days this month."); }

Use if-else structures when you need comprehensive handling of all possible scenarios, with the final else acting as a catch-all for any cases not explicitly covered.


Switch

The switch statement provides a cleaner alternative to lengthy if-else chains when evaluating a single variable against multiple possible values, resulting in more readable and maintainable code.

Example 1: Basic switch structure

switch (season) { case "Summer": print("It's too hot!"); break; case "Winter": print("It's too cold!"); break; default: print("It's just fine!"); }

Use this pattern when you need to compare a single variable against multiple specific values, with each case triggering different actions. The default case handles any values not explicitly covered.


While

A while loop repeatedly executes a block of code as long as a specified condition remains true, providing a flexible way to control repetitive actions based on an ongoing condition.

Example 1: Basic while loop structure

number count = 1; while (count <= 10) { runnerLog(count); count++; }

Use this pattern when you need to repeat a block of code an uncertain number of times, continuing execution as long as a specific condition is met. The loop stops automatically when the condition becomes false, giving precise control over the iteration process.

Related content