PLCcheck

SCL Syntax Reference: IF, CASE, FOR, WHILE, REPEAT

Complete SCL syntax cheat sheet for Siemens TIA Portal. IF/THEN/ELSE, CASE, FOR, WHILE, REPEAT with code examples. For S7-1200 and S7-1500.

·8 min read
SCLsyntaxIFCASEFORWHILEREPEATTIA PortalS7-1500S7-1200Structured TextIEC 61131-3

Diesen Artikel auf Deutsch lesen

SCL Syntax Reference: IF, CASE, FOR, WHILE, REPEAT

SCL (Structured Control Language) is Siemens' implementation of IEC 61131-3 Structured Text (ST). It is a high-level, Pascal-like text language available on S7-300, S7-400, S7-1200, and S7-1500. This page covers all control flow statements.

IF / THEN / ELSE / ELSIF

Simple IF:

IF #Start_Button THEN
    #Motor := TRUE;
END_IF;

IF / ELSE:

IF #Temperature > 80.0 THEN
    #Cooling_Fan := TRUE;
ELSE
    #Cooling_Fan := FALSE;
END_IF;

IF / ELSIF / ELSE (multiple conditions):

IF #Level > 90 THEN
    #Alarm := TRUE;
    #Pump := FALSE;
ELSIF #Level > 70 THEN
    #Alarm := FALSE;
    #Pump := FALSE;
ELSIF #Level < 30 THEN
    #Alarm := FALSE;
    #Pump := TRUE;
ELSE
    #Alarm := FALSE;
    #Pump := FALSE;
END_IF;

Rules:

CASE

Selects one of several code paths based on an integer value:

CASE #Step OF
    0:  // Idle
        #Motor := FALSE;
        #Valve := FALSE;
    1:  // Fill
        #Valve := TRUE;
    2:  // Mix
        #Motor := TRUE;
    3:  // Drain
        #Valve := TRUE;
        #Motor := FALSE;
    ELSE  // Unknown step
        #Alarm := TRUE;
END_CASE;

Multiple values per branch:

CASE #Error_Code OF
    1, 2, 3:    #Category := 1;  // Minor errors
    10, 11, 12: #Category := 2;  // Medium errors
    100..199:   #Category := 3;  // Critical range
    ELSE:       #Category := 0;  // Unknown
END_CASE;

Rules:

FOR Loop

Executes a fixed number of iterations:

FOR #i := 0 TO 9 DO
    #Values[#i] := 0;  // Clear array
END_FOR;

With step size:

FOR #i := 0 TO 100 BY 10 DO
    // Executes for i = 0, 10, 20, ..., 100
END_FOR;

Counting down:

FOR #i := 10 TO 1 BY -1 DO
    // Executes for i = 10, 9, 8, ..., 1
END_FOR;

Rules:

WHILE Loop

Executes as long as a condition is TRUE:

#i := 0;
WHILE #i < 100 AND NOT #Error DO
    #Buffer[#i] := #Input_Data;
    #i := #i + 1;
END_WHILE;

Rules:

REPEAT / UNTIL

Executes at least once, then repeats until condition is TRUE:

REPEAT
    #Retry_Count := #Retry_Count + 1;
    #Success := SEND_DATA();
UNTIL #Success OR (#Retry_Count >= 3)
END_REPEAT;

Rules:

GOTO

Jumps to a label. Available but strongly discouraged:

IF #Error THEN
    GOTO Label_End;
END_IF;
// ... normal code ...
Label_End:
    #Output := FALSE;

Recommendation: Avoid GOTO. Use IF/ELSE, RETURN, or EXIT instead.

RETURN

Exits the current block immediately:

IF NOT #Enable THEN
    RETURN;  // Exit block, skip all remaining code
END_IF;
// ... rest of block only executes if Enable = TRUE

EXIT

Breaks out of the innermost FOR, WHILE, or REPEAT loop:

FOR #i := 0 TO 99 DO
    IF #Values[#i] = #SearchValue THEN
        #Found_Index := #i;
        EXIT;  // Stop searching
    END_IF;
END_FOR;

Assignment

#Output := #Input;                    // Simple copy
#Result := #A + #B * #C;             // Expression
#Motor := #Start AND NOT #Stop;       // Boolean expression
#Text := 'Hello';                     // String assignment

Comments

// Single-line comment

(* Multi-line
   comment *)

Part of the SCL Reference. Maintained by PLCcheck.ai.


Convert Your AWL Code to SCL

PLCcheck Pro analyzes your S5/S7 AWL code and generates SCL equivalents automatically. Upload your program and see the conversion side by side.

Upload code for AWL→SCL conversion → | AWL to SCL Guide →

Related Articles

Analyze your PLC code with AI

PLCcheck Pro explains, documents, optimizes, and migrates PLC code — automatically.

Try PLCcheck Pro →
← Back to Blog

Not affiliated with Siemens AG. S5, S7, STEP 5, STEP 7, and TIA Portal are trademarks of Siemens AG.