mirror of
https://github.com/irmen/prog8.git
synced 2025-01-14 01:29:55 +00:00
warn about for-loop wrapped iteration if loop range is inverted from normal
This commit is contained in:
parent
91e421d961
commit
e0454e95db
@ -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!
|
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)
|
(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
|
Conditional Execution
|
||||||
---------------------
|
---------------------
|
||||||
|
@ -1,18 +1,16 @@
|
|||||||
%import textio
|
%import textio
|
||||||
|
%zeropage basicsafe
|
||||||
|
|
||||||
main {
|
main {
|
||||||
sub start() {
|
sub start() {
|
||||||
uword width
|
ubyte lives=2
|
||||||
uword width2 = 12345
|
ubyte lvs
|
||||||
ubyte ub1
|
|
||||||
ubyte ub2 = 123
|
|
||||||
|
|
||||||
ub1 = ub2 % 32
|
for lvs in 10 to lives {
|
||||||
txt.print_ub(ub1)
|
txt.print_ub(lvs)
|
||||||
|
txt.spc()
|
||||||
|
}
|
||||||
txt.nl()
|
txt.nl()
|
||||||
width = width2 % 32
|
|
||||||
txt.print_uw(width)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user