How-to
Replace a 16-branch ELSIF chain with one indexed array read
Short answer
When A is an INT and B is an array of INT, replace the whole ELSIF chain with IF COUNT >= 1 AND COUNT <= 16 THEN A := B[COUNT - 1]; END_IF. COUNT is one-based while the array runs from B[0] to B[15], so the subtraction maps the selection and the range check keeps the index inside the array.
On this page
A 16-branch ELSIF chain that copies B[0] through B[15] into A is one assignment wearing sixteen conditions. Because A is an INT and B is an array of INT, select the element with the counter itself: COUNT is one-based (COUNT = 1 selects B[0]) while the array runs from B[0] to B[15], so subtract 1 and guard the range between 1 and 16. One line replaces the chain and never addresses outside the array.
Replace the chain with one guarded assignment #
Write the replacement as a single IF statement:
IF (COUNT >= 1) AND (COUNT <= 16) THEN
A := B[COUNT - 1];
END_IF;
The assignment A := B[COUNT - 1] reproduces every branch of the original chain: COUNT = 1 reads B[0], COUNT = 2 reads B[1], COUNT = 8 reads B[7], and COUNT = 16 reads B[15]. A scan that once evaluated up to sixteen comparisons now evaluates two: COUNT >= 1 and COUNT <= 16.
Map COUNT to the array index #
| COUNT | Calculated index | Assigned element |
|---|---|---|
| 1 | 1 - 1 = 0 | A := B[0] |
| 2 | 2 - 1 = 1 | A := B[1] |
| 7 | 7 - 1 = 6 | A := B[6] |
| 8 | 8 - 1 = 7 | A := B[7] |
| 15 | 15 - 1 = 14 | A := B[14] |
| 16 | 16 - 1 = 15 | A := B[15] |
The expression is the whole mapping; no branch table in code is needed. Every row follows the same rule, index = COUNT - 1, from 0 up to 15. When you later extend B to more elements, the only change is the upper limit in the guard.
Why the range check stays #
The referenced array covers only B[0] through B[15]. Without the guard, COUNT = 0 produces index -1 and COUNT = 17 produces index 16; neither index exists. What a controller then does is platform specific: some raise an "Array index out of range" fault at runtime, which on some Logix controllers is a major fault, and some environments run a bounds check on every access. Keep the range check unless other logic guarantees COUNT stays between 1 and 16 on every scan, and the question never comes up. A failed bounds check at runtime is the kind of fault you find at 2 a.m., not in the code review.
If you want the counter selection to drive more than a copy, for example a timer per step, the same indexed approach works there too; see how a REPEAT loop runs in PLC structured text for stepping through the array itself.
Worked example: verifying the replacement for COUNT = 16 #
Inputs: the original chain from the logic being replaced, where COUNT is an INT selector running 1 to 16, A is an INT destination, and B is an array of INT with elements B[0] to B[15].
The rule: a one-based selection value N maps to the zero-based index N - 1, and the assignment runs only when N is inside 1 to 16.
The arithmetic for the last step:
- COUNT = 16, so the index is 16 - 1 = 15.
- The guard evaluates 16 >= 1 (true) AND 16 <= 16 (true), so the assignment executes.
- A receives B[15], the last element, exactly as the original ELSIF COUNT = 16 branch did.
The decision: adopt the guarded one-line assignment, and prove it with a four-step online test before the chain is deleted:
- Put distinct values in B[0] and B[15].
- Set COUNT to 1 and confirm A receives B[0].
- Set COUNT to 16 and confirm A receives B[15].
- Set COUNT to 0 and to 17; with the guard, the assignment does not execute and A keeps its previous value.
If all four steps pass, delete the ELSIF chain. If step 4 ever faults or changes A, the guard was dropped or mistyped; fix the range check, not the array bounds.
Questions asked
What does the controller do when COUNT goes out of range without the guard?
The referenced array covers only B[0] through B[15], so COUNT = 0 gives index -1 and COUNT = 17 gives index 16, and neither index exists. What happens next is controller specific: some platforms raise an array index out of range fault at runtime, which can be a major fault, and some environments perform a bounds check on every access. Keep the range check so the question never arises.
Can I drop the IF and write only the assignment?
Only when other logic in the program guarantees COUNT stays between 1 and 16 on every scan. If an operator entry, a communication write or a counter overrun can push COUNT outside that range, keep the guard; it costs one comparison pair and prevents an invalid index.
Does the same replacement work for arrays of REAL or BOOL?
Yes. The indexed read is independent of the element type: A := B[COUNT - 1] works when A matches the element type of B, whether INT, REAL or another type. Declare A with the same type as the array elements and keep the same range check on COUNT.