Skip to content

Control Structures

Control structures determine the execution flow of a program. Without them, code would simply run line by line. With IF, WHILE, or FOR we can make decisions and repeat instructions. In a PLC‑like environment such as Coldwave Script, two aspects are particularly important: determinism and clarity.

IF / ELSEIF / ELSE

Use IF to make decisions. ELSEIF allows additional branches, ELSE catches the remaining case.

vb
PROGRAM  
    DIM t AS INT = 23  
    IF t < 18 THEN  
        PRINT "Day tariff"  
    ELSEIF t < 22 THEN  
        PRINT "Evening tariff"  
    ELSE  
        PRINT "Night tariff"  
    END IF  
END PROGRAM

Output: Night tariff

WHILE, FOR

WHILE executes instructions as long as a condition holds.

vb
PROGRAM  
    DIM i AS INT = 0  
    DO WHILE i < 3  
        PRINT i  
        i = i + 1  
    LOOP  
END PROGRAM

' Output: 0 1 2

FOR runs from a start value to an end value.

vb
PROGRAM  
    DIM k AS INT  
    FOR k = 1 TO 3  
      PRINT k  
    NEXT  
END PROGRAM

' Output: 1 2 3

Advantage of FOR: The maximum number of iterations is immediately visible — important for WCET calculations.

DO / WHILE / UNTIL

There are four valid variants of DO loops. They differ based on whether the condition is checked before or after the loop body, and whether the loop runs “while” or “until” a condition is true.

vb
' Condition before the block (pre‑test)
DO WHILE cond
    ' Instructions
LOOP

DO UNTIL cond
    ' Instructions
LOOP

' Condition after the block (post‑test)
DO
    ' Instructions
LOOP WHILE cond

DO
    ' Instructions
LOOP UNTIL cond
  • In the pre‑test variants (DO WHILE / DO UNTIL), the condition is checked before the first iteration.
  • In the post‑test variants (LOOP WHILE / LOOP UNTIL), the condition is checked after the block; the loop always runs at least once.

A DO ... LOOP without WHILE/UNTIL condition is not allowed because it would result in an endless loop.

Loop Control: EXIT / CONTINUE

Two statements let you influence the flow of an active loop:

  • EXIT — leaves the innermost surrounding loop immediately. Execution continues after the loop.
  • CONTINUE — skips the rest of the current iteration and proceeds with the next one.

Both statements are only allowed inside a loop body. Using them elsewhere is a compile error (SCRIPT_ERR_EXIT_OUTSIDE_LOOP).

Example: early exit on a condition

vb
PROGRAM
    DIM i AS INT
    FOR i = 1 TO 100
        IF sensorError THEN
            EXIT            ' leave the FOR immediately
        END IF
        PRINT i
    NEXT
END PROGRAM

Example: skip one iteration

vb
PROGRAM
    DIM i AS INT
    FOR i = 1 TO 10
        IF (i MOD 2) = 0 THEN
            CONTINUE        ' skip even values
        END IF
        PRINT i             ' prints 1 3 5 7 9
    NEXT
END PROGRAM

Restriction on CONTINUE in DO/WHILE/UNTIL: CONTINUE is only permitted inside FOR. Using it in a DO/WHILE/UNTIL loop is rejected at compile time. The reason is determinism — see the next section.

SELECT / CASE

SELECT is a compact alternative to long IF/ELSEIF chains.
The expression after SELECT is evaluated once, each CASE line compares using =.

Multi‑line variant

vb
PROGRAM
    DIM mode AS INT = 1

    SELECT mode
    CASE 0
        PRINT "Off"
    CASE 1
        PRINT "Automatic"
    CASE 2
        PRINT "Manual"
    CASE ELSE
        PRINT "Unknown mode"
    END SELECT
END PROGRAM
  • The first matching CASE branch is executed.
  • CASE ELSE is used when no other CASE matches.

Single‑line variant

vb
PROGRAM
    DIM mode AS INT = 0

    SELECT mode
    CASE 0: PRINT "Off"
    CASE 1: PRINT "Automatic"
    CASE ELSE: PRINT "Manual"
    END SELECT
END PROGRAM

Single‑line CASE statements are useful for short actions such as debugging output.

Loop Constraints (Safety / WCET)

In safety‑critical systems, it must be clear how many iterations a loop can take at most. Endless loops are forbidden. Every loop must have an upper bound.

Example: Safe WHILE loop

vb
PROGRAM  
    DIM i AS INT = 0  
    DO WHILE (i < 10) AND (SENSOR_OK)  
        ' Work ...
        i = i + 1  
    LOOP  
END PROGRAM

EXIT and CONTINUE do not compromise the iteration bound of a FOR loop — the upper bound given by TO still holds. CONTINUE is forbidden in DO/WHILE/UNTIL precisely because it would make the iteration count harder to prove statically.

Examples

Threshold monitoring with hysteresis:

vb
PROGRAM  
    DIM x AS INT = 55  
    DIM alarm AS BOOL = FALSE  
    IF (x > 60) THEN  
        alarm = TRUE  
    END IF  
    IF (x < 50) THEN  
        alarm = FALSE  
    END IF  
    PRINT alarm ' Output: FALSE  
END PROGRAM

Blinking pattern (simplified example using a loop):

vb
PROGRAM  
    DIM i AS INT  
    FOR i = 1 TO 4  
        PRINT "LED ON"  
        PRINT "LED OFF"  
    NEXT
END PROGRAM

Simple state machine:

vb
PROGRAM  
    DIM state AS INT = 0  
    IF state = 0 THEN  
        PRINT "Init"  
        state == 1  
    ELSEIF state = 1 THEN  
        PRINT "Running"  
        state == 2  
    ELSE  
        PRINT "Stopped"  
    END IF  
END PROGRAM

Summary

  • IF/ELSE makes decisions, WHILE/FOR and DO loops repeat instructions.
  • Bounded loops are required for safe and deterministic behavior.
  • Examples show how control structures can implement alarms, blinking patterns, and state machines.