Updated date:

Introduction to the PLC Language: Structured Text


About Structured Text

Structured text is one of (if not, the most) powerful of the available IEC languages. It allows complex, conditional code to be written much easier than any of the other languages.

The following functions are available in Structured Text:

  • FOR - Iterate through nested code a number of times
  • WHILE - Execute the same nested code until the while condition is false
  • IF - Execute nested code only if the condition is true
  • ELSE - Execute an alternative section of code if the IF condition is false
  • ELSIF - Conditional Else function, if the IF condition is false, but the ELSIF condition is true, execute the nested code. ELSIF can be stacked multiple times, and combined with an ELSE at the end to ensure code runs in the event of all conditions returning false. More on stacked conditions later!
  • CASE Statement - Execute a particular case, depending on the value of a declared variable

Structured Text lends itself heavily to complex algorithms, long mathematical functions, array manipulation and repetitive tasks. I often find myself using ST (structured text) in function blocks that iterate over arrays.

Programming Environment

ST is no different to any of the other IEC languages, and every editor displays it slightly differently.

The ST environment is extremely simple, it's essentially a blank text file. In most editors, the lines are numbered for convenience of referencing code in documentation.

	Start:=FALSE; //Remove Start Latch
	WHILE Initialised = FALSE DO
		System_Prime:=TRUE; //Set Output On For System Primer
	System_Prime:=FALSE; //Turn Off Output For System Primer Once Initialised

Consider the above example, let's analyse this line by line

  1. IF Statement - If the variable "Start" is TRUE then execute all code between the IF and the END_IF
  2. Set the variable "Start" to FALSE. This stops "Start" from remaining TRUE once we have started executing the IF statement. Ensuring the IF Statement only executes once
  3. WHILE Statement - Whilst the variable "Initialised" is FALSE, DO the nested code repeatedly.
  4. Set the variable "System_Prime" to true
  5. END_WHILE - Ends the WHILE statement
  6. Set the variable "System_Prime" to false
  7. END_IF - Ends the IF Statement


Structured Text is extremely powerful, however it does have some "issues" that need to be understood in order to use it correctly.

WHILE statements and FOR statements can cause some dangerous conditions if the above is not remembered or understood. Consider the earlier example, when the WHILE statement is repeating, no other code is executing, before or after the while loop.

This can be demonstrated by updating the code to the following:

	Start:=FALSE; //Remove Start Latch
	WHILE Initialised = FALSE DO
		System_Prime:=TRUE; //Set Output On For System Primer
	System_Prime:=FALSE; //Turn Off Output For System Primer Once Initialised

"Var1" will be initialised to the value 0 before the IF statement is executed, however when the WHILST statement is running, line 8 will not be executed, even though the IF statement is still executing.

Most PLCs use a single thread per task, meaning the code will execute sequentially one line at a time.

So how do we stop this from happening? If we had some critical outputs, we don't want code to block execution until the WHILE condition is complete? There are two options

  1. Simply don't use while statements. Replace them with IF statements that do not block execution. In most cases IF statements can reproduce the same desired outcome with a little extra coding, without the side effects of blocking code
  2. Run the WHILE code in a separate task with a low priority, this would not block execution on other tasks

IF Statements

IF statements are probably the most used function in ST and the most common reason why ST would be chosen for the POU / Function Block in development.

Take a look at the example below

IF S1 = 0 THEN
	S1:= S1+1;
	S1:= 2;
	S1:= 3;

This example shows a 4 state IF statement that is guaranteed to execute 1 state only.

  • S1 = 0
  • S1 = 1
  • S1 = 2
  • S1 = Anything else

The ELSIF statement will only execute if the IF or ELSIF statement above it has NOT executed.

The ELSE statement will always execute if none of the statements above have executed

CASE Statement

CASE Statements are very similar to IF statements, however they define the state to run without having to pass through every ELSIF statement.

Take a look at the code below:


This CASE statement provides almost the same functionality as the previous IF example, the only difference is there is not a "Anything" case that captures any undefined conditions. CASE statements are explicit, the case must exist else it will not be actioned.

Common uses of CASE is to enforce a State Machine type architecture


Structured Text is extremely powerful and certainly has it's place in application design. Favoured for complex tasks, it's flexibility is superior to all other IEC languages, it is easily read, but can be confusing to beginners, especially with the caveats that other languages do not suffer with.

ST should be the language that is learnt second to Ladder (As ladder is most common), it will open up possibilities in functions and function blocks that are extremely difficult to achieve in Ladder alone


Liam (author) from UK on January 16, 2020:


Two things

1. You have no closing End_If for your stop statement

2. You can't use ":=" in an if statement. ":=" Sets a variable to a value, use "=" instead

Birzhan on January 16, 2020:

Hello, Liam!

I am learning to code on ST via OpenPCS

Where is my mistake in this simple logic?

The code is below.

Thank you!


start AT%I0.4 : BOOL;

stop AT%I0.5 : BOOL;

pump AT%Q0.2;


if start:=TRUE then



if stop:=TRUE then


PLCGuru on December 30, 2018:

Hi Liam, I've read a couple of your articles now and well done! My name is Fred and I am the founding member and site moderator over at PLCGurus.NET (https://plcgurus.net). We'd love to get you on-board over at the site as a member and active participant in our community (100% free). And perhaps you would consider writing a guest post with us that will be seen by a community who is 100% interested in industrial automation and control systems. Keep up the good work!

Liam (author) from UK on December 06, 2018:

It's meaningless when taken literally in this example yes. Its demonstrating how while loops can block execution and get stuck in loops... Perhaps I haven't made that clear enough.

However, it is taken from an actual project, where running in a sperate freewheeling task, was a POU that did set initialized to true when conditions were met, which would have ended the while loop.

This example does not state what declaration scope the variables are in. You hsve just taken face logic value. This article is just an introduction to structured text and the syntaxes used.

Related Articles