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.
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:
- Condition must evaluate to BOOL (TRUE/FALSE)
- Every IF must have a matching END_IF
- Semicolons after each statement
- ELSIF (not ELSEIF or ELSE IF)
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:
- Selector must be an integer type (INT, DINT, SINT, USINT, etc.) — NOT REAL, NOT STRING
- Case values must be constants (no variables)
- Range notation
100..199supported in TIA Portal - ELSE is optional but recommended
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:
- Loop counter must be an integer variable
- Do NOT modify the loop counter inside the loop body (Siemens Programming Guideline explicitly warns against this)
- Start, end, and step values are evaluated once at loop entry
- Use EXIT to break out of a loop early
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:
- Condition checked BEFORE each iteration (may execute zero times)
- You must ensure the condition eventually becomes FALSE — infinite loops cause CPU watchdog timeout
- Use EXIT for early termination
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:
- Condition checked AFTER each iteration (always executes at least once)
- Loop ends when condition becomes TRUE (opposite of WHILE!)
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.
Related Articles
Converting Ladder Logic to Structured Text: Why and How
When and how to convert PLC ladder logic (LD/KOP/LAD) to Structured Text (ST/SCL). Covers the benefits, conversion patterns, what to keep in ladder, and practical examples.
8 min read
migration-guideS7-300 to S7-1500 Migration: Complete Guide
Step-by-step guide for migrating Siemens S7-300 PLC programs to S7-1500 using TIA Portal. Covers hardware mapping, software migration wizard, optimized data blocks, AWL→SCL conversion, and common pitfalls.
15 min read
migration-guideSTL/AWL Deprecation in S7-1500: Why You Must Convert to SCL
Why AWL/STL runs only in emulation mode on S7-1500, what that means for performance and maintainability, and how to convert your STL code to SCL. Includes conversion strategy and code examples.
10 min read
Analyze your PLC code with AI
PLCcheck Pro explains, documents, optimizes, and migrates PLC code — automatically.
Try PLCcheck Pro →Not affiliated with Siemens AG. S5, S7, STEP 5, STEP 7, and TIA Portal are trademarks of Siemens AG.