rusty icon indicating copy to clipboard operation
rusty copied to clipboard

for loop executes once when condition is already met

Open tisis2 opened this issue 2 months ago • 0 comments

Discussed in https://github.com/PLC-lang/rusty/discussions/1206

Originally posted by kanishka-karan April 19, 2024 Hi

We are using decrement for loop :

{external}
FUNCTION printf : DINT
VAR_INPUT {ref}
    format : STRING;
END_VAR
VAR_INPUT
    args: ...;
END_VAR
END_FUNCTION

program inProgram
   var
      i : DINT;
      end : DINT;
   end_var
    end := -1;
    printf('end: %d$N', end);
    for i := end to 0 by -1 do
        printf('for end: %d$N', end);
    end_for;

end_program

The output of the program is :

end: -1
for end: -1

It shows that the for loop code is executing once even if end is equal to -1. We think it should not run the code inside for loop, as the condition is not met.

We interpreted the Structured text for loop

 for i := end to 0 by -1 do

as in C/C++ as

for (i= end ; i >= 0; i-- )

Please let us know if we our understanding is correct.

Thank You

tisis2 avatar Apr 19 '24 06:04 tisis2