warn about for-loop wrapped iteration if loop range is inverted from normal

This commit is contained in:
Irmen de Jong 2021-04-08 22:54:47 +02:00
parent 91e421d961
commit e0454e95db
2 changed files with 15 additions and 9 deletions

View File

@ -455,6 +455,14 @@ Breaking out of a loop prematurely is possible with the ``break`` statement.
after the loop without first assigning a new value to it!
(this is an optimization issue to avoid having to deal with mostly useless post-loop logic to adjust the loop variable's value)
.. warning::
For efficiency reasons, it is assumed that the ending value of the for loop is actually >= the starting value
(or <= if the step is negative). This means that for loops in prog8 behave differently than in other
languages if this is *not* the case! A for loop from ubyte 10 to ubyte 2, for example, will iterate through
all values 10, 11, 12, 13, .... 254, 255, 0 (wrapped), 1, 2. In other languages the entire loop will
be skipped in such cases. But prog8 omits the overhead of an extra loop range check and/or branch for every for loop
by assuming the normal ranges.
Conditional Execution
---------------------

View File

@ -1,18 +1,16 @@
%import textio
%zeropage basicsafe
main {
sub start() {
uword width
uword width2 = 12345
ubyte ub1
ubyte ub2 = 123
ubyte lives=2
ubyte lvs
ub1 = ub2 % 32
txt.print_ub(ub1)
for lvs in 10 to lives {
txt.print_ub(lvs)
txt.spc()
}
txt.nl()
width = width2 % 32
txt.print_uw(width)
}
}