News:

Let's find out together what makes a PIC Tick!

Main Menu

For ... Next Step -1

Started by trastikata, Aug 10, 2021, 07:33 AM

Previous topic - Next topic

trastikata

Just to be aware of decreasing loops ...

QuoteFor i = 9 to 0
  'do something
Next

took me an hour till I spotted the error why my program is not working as supposed :)

Interestingly If the loop requires larger steps or variables, for example -4 or -a, then I don't forget it however I miss the "Step -1" for the second time with simple negative counters.

top204

#1
The negative Step value is standard across virtually all versions of BASIC when the loop decrements, since the 1970s. Unfortunately, the compiler cannot detect the issue because if it has variables as the parameters it has no idea what is in them, so it also does not look for constant values either. Otherwise, it might confuse users why it can detect one set of parameters but not others. Remember, some beginners do not quite understand the difference between a constant and a variable, until they have programmed for a while.

The Positron compilers have a couple of new directives that may help some people, and I am going to add them to the manuals as soon as I can find some time to do so:

For MyVariable = 9 DownTo 0
' Code Here
Next

or

For MyVariable = 0 UpTo 9
' Code Here
Next

Notice the DownTo and UpTo directives? They do the same as Step -1 and Step 1, but, hopefully, will make the code easier to understand and write for new users. They will also work for variable based loops within For-Next

trastikata

Quote from: top204 on Aug 10, 2021, 12:22 PMNotice the DownTo and UpTo directives? They do the same as Step....

Les, your work and help is much appreciated. This is excellent addition to the compiler and I am sure when I get used to using the new directives, it will prevent such omissions on my, the user, side in future.

top204

I check out the For-Next section of the parser and see if I can detect the min/max parameter types and give an error if they are both constants and do not fit with the Step used, or no Step used.

It sounds simple, however if you could see the parser code I created for For-Next, you would think again. LOL But, I'll take a look.