Uploaded November 2026 | Updated November 2026, 3 hours ago
Structured text IF ELSIF ELSE, CASE and FOR in a PLC: multi-level analog comparisons, state machines and arrays, all cleaner than the ladder equivalent.
โก QUICK ANSWER
IF, ELSIF and ELSE chains are where structured text earns its place in a PLC program. Three analog setpoints in ladder means three separate compare rungs, and stopping the bands from overlapping is your problem to solve. In ST it is one chain that evaluates in order and stops at the first true condition, so only one branch can ever run - the bands are mutually exclusive by construction. A state machine in ladder means a step register and a rung for every transition. In ST it is a CASE statement where each step is its own labelled block:
CASE Step OF
0: IF X1 THEN Step := 1; END_IF;
1: Y1 := TRUE; IF X2 THEN Step := 2; END_IF;
2: Y1 := FALSE; Step := 0;
END_CASE;
The whole sequence reads in one place. And processing eight readings in ladder means eight copies of the same rung; in ST it is a FOR loop over an array. One caution that comes with the power: the program still runs top to bottom every scan, so never use a WHILE loop to wait for a condition - use TON, TOF and state variables for timing. Full guide, with the analog comparison code, on the blog.
๐ FULL WRITTEN GUIDE
accautomation.ca/acc-plc-simulator-how-to-use-structured-text-st-programming
๐ฅ๏ธ OPEN THE SIMULATOR AND PRESS ST
accautomation.ca/simulator
โถ WATCH THE FULL VIDEO
youtu.be/B0UDgJnE3DQ
๐ WHAT YOU'LL LEARN
โ Write multi-level analog comparisons as one IF, ELSIF, ELSE chain, where only the first true branch runs - no overlapping bands to interlock against
โ Build a state machine with a CASE statement, with each step as a labelled block instead of a rung per transition
โ Process an array of readings with a FOR loop rather than copying the same rung eight times
โ Know that WHILE and REPEAT exist too, and why waiting in a loop is the one thing never to do in a PLC
โ Pick the right language per job - ladder for plain interlocking, structured text for comparisons, sequencing and data
โ FREQUENTLY ASKED
Q: Why is an IF ELSIF chain better than compare instructions in ladder?
A: Because only one branch can execute. In ladder, three compare rungs are three independent tests, so overlapping bands are your problem to interlock against. An IF, ELSIF, ELSE chain evaluates in order and stops at the first true condition, which makes the bands mutually exclusive by construction.
Q: Can I use a WHILE loop to wait for an input?
A: No, and this is the most important rule in structured text on a PLC. The whole program runs top to bottom every scan, so a loop that waits never lets the scan complete. Use a TON, a TOF or a state variable instead. FOR loops are fine because they are bounded and finish within the scan.
Q: What is a CASE statement used for in a PLC?
A: Sequencing. A step variable holds the current state, and each CASE block contains what happens in that step and the condition that moves to the next one. Same pattern as a ladder step register, but the whole sequence is readable in one place.
๐ The full guide, with every statement and function block: accautomation.ca/acc-plc-simulator-how-to-use-structured-text-st-programming
๐ MORE RESOURCES
โถ Try the free simulator: accautomation.ca/simulator
โถ How I learned structured text in the simulator: youtu.be/B0UDgJnE3DQ
โถ PLC Beginners Guide: accautomation.ca/programming/plc-beginners-guide
โถ Free eBooks: accautomation.ca (subscribe for download)
๐ RECOMMENDED HARDWARE
โถ Shop: automationdirect.com
โถ Parts Guide: accautomation.ca/purchase-find-parts
๐ฌ Questions? Drop a comment below.
๐ New tutorials every Monday โ Subscribe and hit the bell!
#PLC #LadderLogic #IndustrialAutomation #PLCProgramming #ACCAutomation #StructuredText #IEC61131 #AnalogIO #LearnPLC #Shorts
Regards,
Garry
ACC Automation
accautomation.ca
Structured text IF ELSIF ELSE, CASE and FOR in a PLC: multi-level analog comparisons, state machines and arrays, all cleaner than the ladder equivalent.
โก QUICK ANSWER
IF, ELSIF and ELSE chains are where structured text earns its place in a PLC program. Three analog setpoints in ladder means three separate compare rungs, and stopping the bands from overlapping is your problem to solve. In ST it is one chain that evaluates in order and stops at the first true condition, so only one branch can ever run - the bands are mutually exclusive by construction. A state machine in ladder means a step register and a rung for every transition. In ST it is a CASE statement where each step is its own labelled block:
CASE Step OF
0: IF X1 THEN Step := 1; END_IF;
1: Y1 := TRUE; IF X2 THEN Step := 2; END_IF;
2: Y1 := FALSE; Step := 0;
END_CASE;
The whole sequence reads in one place. And processing eight readings in ladder means eight copies of the same rung; in ST it is a FOR loop over an array. One caution that comes with the power: the program still runs top to bottom every scan, so never use a WHILE loop to wait for a condition - use TON, TOF and state variables for timing. Full guide, with the analog comparison code, on the blog.
๐ FULL WRITTEN GUIDE
accautomation.ca/acc-plc-simulator-how-to-use-structured-text-st-programming
๐ฅ๏ธ OPEN THE SIMULATOR AND PRESS ST
accautomation.ca/simulator
โถ WATCH THE FULL VIDEO
youtu.be/B0UDgJnE3DQ
๐ WHAT YOU'LL LEARN
โ Write multi-level analog comparisons as one IF, ELSIF, ELSE chain, where only the first true branch runs - no overlapping bands to interlock against
โ Build a state machine with a CASE statement, with each step as a labelled block instead of a rung per transition
โ Process an array of readings with a FOR loop rather than copying the same rung eight times
โ Know that WHILE and REPEAT exist too, and why waiting in a loop is the one thing never to do in a PLC
โ Pick the right language per job - ladder for plain interlocking, structured text for comparisons, sequencing and data
โ FREQUENTLY ASKED
Q: Why is an IF ELSIF chain better than compare instructions in ladder?
A: Because only one branch can execute. In ladder, three compare rungs are three independent tests, so overlapping bands are your problem to interlock against. An IF, ELSIF, ELSE chain evaluates in order and stops at the first true condition, which makes the bands mutually exclusive by construction.
Q: Can I use a WHILE loop to wait for an input?
A: No, and this is the most important rule in structured text on a PLC. The whole program runs top to bottom every scan, so a loop that waits never lets the scan complete. Use a TON, a TOF or a state variable instead. FOR loops are fine because they are bounded and finish within the scan.
Q: What is a CASE statement used for in a PLC?
A: Sequencing. A step variable holds the current state, and each CASE block contains what happens in that step and the condition that moves to the next one. Same pattern as a ladder step register, but the whole sequence is readable in one place.
๐ The full guide, with every statement and function block: accautomation.ca/acc-plc-simulator-how-to-use-structured-text-st-programming
๐ MORE RESOURCES
โถ Try the free simulator: accautomation.ca/simulator
โถ How I learned structured text in the simulator: youtu.be/B0UDgJnE3DQ
โถ PLC Beginners Guide: accautomation.ca/programming/plc-beginners-guide
โถ Free eBooks: accautomation.ca (subscribe for download)
๐ RECOMMENDED HARDWARE
โถ Shop: automationdirect.com
โถ Parts Guide: accautomation.ca/purchase-find-parts
๐ฌ Questions? Drop a comment below.
๐ New tutorials every Monday โ Subscribe and hit the bell!
#PLC #LadderLogic #IndustrialAutomation #PLCProgramming #ACCAutomation #StructuredText #IEC61131 #AnalogIO #LearnPLC #Shorts
Regards,
Garry
ACC Automation
accautomation.ca










