Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for loop executes once when condition is already met #1207

Open
tisis2 opened this issue Apr 19, 2024 Discussed in #1206 · 0 comments · May be fixed by #1248
Open

for loop executes once when condition is already met #1207

tisis2 opened this issue Apr 19, 2024 Discussed in #1206 · 0 comments · May be fixed by #1248
Labels

Comments

@tisis2
Copy link

tisis2 commented Apr 19, 2024

Discussed in #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

mhasel added a commit that referenced this issue Jun 17, 2024
This PR fixes for loops executing once when the predicate already should not be met for decrementing loops.
I have also re-implemented the codegen logic for for-loops, resulting in fewer predecessors and hopefully more
readable IR.

Resolves #1207
mhasel added a commit that referenced this issue Jun 17, 2024
This PR fixes for loops executing once when the predicate already should not be met for decrementing loops.
I have also re-implemented the codegen logic for for-loops, resulting in fewer predecessors and hopefully more
readable IR.

Resolves #1207
mhasel added a commit that referenced this issue Jun 17, 2024
This PR fixes for loops executing once when the predicate already should not be met for decrementing loops.
I have also re-implemented the codegen logic for for-loops, resulting in fewer predecessors and hopefully more
readable IR.

Resolves #1207
mhasel added a commit that referenced this issue Jun 17, 2024
This PR fixes for loops executing once when the predicate already should not be met for decrementing loops.
I have also re-implemented the codegen logic for for-loops, resulting in fewer predecessors and hopefully more
readable IR.

Resolves #1207
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant