I'm new to PLC logic and would like to know a more efficient way to change the following statement.
A is INT, B is an array of INT.
IF COUNT = 1 THEN A := B[0];
ELSIF COUNT = 2 THEN A := B[1];
ELSIF COUNT = 3 THEN A := B[2];
ELSIF COUNT = 4 THEN A := B[3];
ELSIF COUNT = 5 THEN A := B[4];
ELSIF COUNT = 6 THEN A := B[5];
ELSIF COUNT = 7 THEN A := B[6];
ELSIF COUNT = 8 THEN A := B[7];
ELSIF COUNT = 9 THEN A := B[8];
ELSIF COUNT = 10 THEN A := B[9];
ELSIF COUNT = 11 THEN A := B[10];
ELSIF COUNT = 12 THEN A := B[11];
ELSIF COUNT = 13 THEN A := B[12];
ELSIF COUNT = 14 THEN A := B[13];
ELSIF COUNT = 15 THEN A := B[14];
ELSIF COUNT = 16 THEN A := B[15];
END_IF;
IF COUNT>=1 AND COUNT<=16 THEN
A := B[COUNT-1];
END_IF
The IF
statement is not necessary but ensures that you don't put an invalid array index into B
if COUNT
gets out of range.