PLC-lang / rusty

Structured Text Parser and LLVM Frontend
GNU Lesser General Public License v3.0
181 stars 47 forks source link

for loop executes once when condition is already met #1207

Open tisis2 opened 1 month ago

tisis2 commented 1 month ago

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 : ```iecst {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 : ```iecst 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 ```iecst for i := end to 0 by -1 do ``` as in C/C++ as ```cpp for (i= end ; i >= 0; i-- ) ``` Please let us know if we our understanding is correct. Thank You