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 repeating #178

Open
em00k opened this issue Oct 9, 2018 · 4 comments
Open

FOR LOOP repeating #178

em00k opened this issue Oct 9, 2018 · 4 comments
Assignees
Labels
feature Feature request

Comments

@em00k
Copy link

em00k commented Oct 9, 2018

I find this for loop repeats, only happens if to is set to 255

for x = 0 to 255 
	print at 0,0;x
	pause 1
next x 
pause 0
boriel added a commit that referenced this issue Nov 13, 2018
Bump version: 1.8.7 → 1.8.8

Approved-by: Jose Rodriguez <[email protected]>
@ghost
Copy link

ghost commented Apr 4, 2019

That's to be expected if x is of type UByte.
By default, if you don't DIM x it will assign the shortest / narrowest type (UByte in this case).

FOR loops like the above means the loop must enter when x = 255, and stop when x = 256. Since x is of type UByte, it will never reach 256. Hence the infinite loop.

EDIT: I repost my comment because the previous response has been lost :(

@em00k
Copy link
Author

em00k commented Jan 19, 2021

Sorry ignore my last post on this.

@boriel boriel self-assigned this Nov 3, 2024
@boriel boriel added the feature Feature request label Nov 3, 2024
@boriel
Copy link
Collaborator

boriel commented Nov 3, 2024

I've been thinking of an alternate (and efficient) way to implement this, by using Carry Flag (at least for 8 bits variables).
Maybe in the near future I could come up with a better solution.
In the mean time, the loop above can be rewritten as:

Dim x as UByte
Let x = 0
Do
  Print At 0, 0; x
  Pause 1
  Let x = x + 1
Loop Until x = 0  : REM yes, overflow

@boriel
Copy link
Collaborator

boriel commented Nov 28, 2024

Alternatively, the initial FOR loop will cause the Z80's Carry Flag to be set. Maybe I can take advantage of this and detect the FOR loop ending!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request
Projects
None yet
Development

No branches or pull requests

2 participants