Appearance
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 PROGRAMOutput: 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.
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
CASEbranch is executed. CASE ELSEis 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 PROGRAMSingle‑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 PROGRAMExamples
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 PROGRAMBlinking pattern (simplified example using a loop):
vb
PROGRAM
DIM i AS INT
FOR i = 1 TO 4
PRINT "LED ON"
PRINT "LED OFF"
NEXT i
END PROGRAMSimple 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 PROGRAMSummary
- 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.