Skip to content

for-loop runtime behaviour when STEP changes signage in loop body #1252

@mhasel

Description

@mhasel

Considering the following example, how many iterations do we expect? Currently, we determine the loop-terminating predicate depending on whether or not the STEP variable is positive or negative upon entering the loop, however there are alternative approaches which drastically change the behaviour.

    FUNCTION main : DINT
    VAR
        i, step, temp, iteration: DINT;
    END_VAR
        step := 1;
        iteration := 0;
        FOR i := 3 TO 10 BY step DO
            step := (step + 1) * -2;
            // i:     3, -1,   5, -9, 17
            // step: -4,  5, -14, 26
            iteration := iteration + 1;
        END_FOR
        
        main := iteration;
    END_FUNCTION

We should determine how for-loops behave in codesys when the statement parameters are changed in the loop-body and reevaluate our approach taking those findings into consideration.

Original discussion:
Originally posted by @mhasel in #1248 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions