Loop index

A forums to allow bugs and problems with Flowcode v7 to be reported and resolved.

Moderator: Benj

mnf
Valued Contributor
Valued Contributor
Posts: 493
Joined: Wed May 31, 2017 11:57 am
Has thanked: 52 times
Been thanked: 278 times
Contact:

Loop index

Postby mnf » Mon Mar 19, 2018 11:33 pm

Hi,

I've seen similar to this reported back in 2014.
A loop with a count > 255 needs a word as the index and this indeed now works when using an integer count.
However if using a variable as the loop index then flowcode only assigns a byte variable.

[img]
loop.png
loop.png (8.02 KiB) Viewed 726 times

[/img]

Generates (correctly)

Code: Select all

MX_UINT16 FCLV_LOOP1;
.
.
    for (FCLV_LOOP1=0; (FCLV_LOOP1)<(1000); (FCLV_LOOP1)++)


Whereas
[img]
loop2.PNG
loop2.PNG (6.47 KiB) Viewed 726 times

[/img]

Where x is greater than 8 bits generates the following C

Code: Select all

MX_UINT8 FCLV_LOOP1;

.
.
    for (FCLV_LOOP1=0; (FCLV_LOOP1)<(FCV_X); (FCLV_LOOP1)++)


Which of course fails if x > 255 - FCLV_LOOP1 should mirror the type of 'x'

A workaround is easy - using a user-defined loop variable in a 'for' loop or a 'repeat until' loop.

It can catch the unwary however.....

Martin
These users thanked the author mnf for the post:
Benj (Tue Mar 20, 2018 12:29 pm)
Rating: 5.56%
 

User avatar
Benj
Matrix Staff
Posts: 14151
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Loop index

Postby Benj » Tue Mar 20, 2018 12:30 pm

Thanks Martin,

Yes this has caught me out too, I'll flag it up as a bug in the system.