Selection & jump (RETURN, EXIT, CONTINUE, GOTO)

RETURN

Leaves the current POU immediately. In a FUNCTION, the return value must have been assigned to the function name already; otherwise it carries the type’s default value.

PROGRAM PLC_PRG
    IF NOT xEnable THEN
        (* skip the rest of this scan *)
        RETURN;
    END_IF;
    DoMainWork();
END_PROGRAM

In an FB, RETURN simply ends the call; outputs keep their last assigned values.

In a FUNCTION, RETURN ends the call; the value of the function-name variable at the moment of RETURN is what the caller receives.

FUNCTION FC_Clamp : INT
    VAR_INPUT iValue : INT; iMin : INT; iMax : INT; END_VAR
    IF iValue < iMin THEN
        FC_Clamp := iMin;
        RETURN;                  (* short-circuit *)
    END_IF;
    IF iValue > iMax THEN
        FC_Clamp := iMax;
        RETURN;
    END_IF;
    FC_Clamp := iValue;
END_FUNCTION

EXIT

Terminates the innermost enclosing loop (FOR, WHILE, or REPEAT) immediately. Execution continues with the statement following the loop’s END_FOR / END_WHILE / END_REPEAT.

FOR i := 1 TO 100 DO
    IF arr[i] = TARGET THEN
        iFound := i;
        EXIT;          (* leave the FOR — i and any later code skipped *)
    END_IF;
END_FOR;

EXIT only escapes one level. Nested loops need an EXIT per level, or a flag-and-break pattern:

xDone := FALSE;
FOR i := 1 TO 10 DO
    FOR j := 1 TO 10 DO
        IF matrix[i, j] = TARGET THEN
            xDone := TRUE;
            EXIT;
        END_IF;
    END_FOR;
    IF xDone THEN EXIT; END_IF;
END_FOR;

CONTINUE

Skips the rest of the current loop iteration and advances to the next. In FOR, the loop variable advances by BY (or +1) and the next iteration starts. In WHILE / REPEAT, the condition is re-tested.

FOR i := 1 TO 100 DO
    IF arr[i] = 0 THEN
        CONTINUE;       (* skip zero entries *)
    END_IF;
    iSum := iSum + arr[i];
END_FOR;

CONTINUE was introduced in IEC 61131-3 third edition (2013); older matiec versions may not accept it. ForgeIEC’s matiec build does.

GOTO — NOT in IEC 61131-3 third edition

The third edition (2013) removed the labelled GOTO statement. matiec does not accept it. Legacy code from older vendor toolchains that uses GOTO needs restructuring on import.

Common GOTO → standard-ST migrations:

GOTO useReplacement
Skip ahead inside a loopCONTINUE
Escape a loop earlyEXIT
Escape multiple loop levelsflag + EXIT per level
Multi-way dispatch on a valueCASE
Step machineCASE OF iStep with iStep advanced by transitions
Error handling / cleanuprestructure with IF + RETURN

IEC reference

  • RETURN, EXIT: clause 6.6.3.7 of IEC 61131-3 third edition (2013).
  • CONTINUE: introduced in third edition; same clause.
  • GOTO: deprecated in second edition, removed in third edition.

matiec conformance

RETURN, EXIT, CONTINUE implemented per the standard. No GOTO support — see the migration table above for the common replacements.