For counting backwards

edmunds

Senior Member
Dear all,

My mind should must be off by now and the best thing I could be doing is heading for some beers by now :).

How do you reverse a for loop? Make it count backwards. Not from 0 to 40, but from 40 to 0, for an example?


Thank you for your time,

Edmunds
 

Dippy

Moderator
Are you talking about the For/Next loop?
Step -1 will decrement by 1.
It's not very obvious in the online Manual but is mentioned in the For/Next section.
A tinkering with the text would make it clearer.
 

edmunds

Senior Member
Are you talking about the For/Next loop?
Step -1 will decrement by 1.
It's not terribly clear in the online Manual but is mentioned in the For/Next section.
A tinkering with the text would make it clearer.
Yes, that one. Thank you.

Edmunds
 

Technical

Technical Support
Staff member
for b1 = 40 to 0 step -1
or
for b1 = 40 downto 0

both do the same thing, take your pick.
 

Circuit

Senior Member
for b1 = 40 to 0 step -1
or
for b1 = 40 downto 0

both do the same thing, take your pick.
Wow, this shows how far Manual 2 is getting left behind from the on-line syntax guide; never having heard the "downto" syntax I searched the manuals in vain but there it is in the on-line guide, making me wonder what else I am missing. I know that we are probably waiting for some PICAXE editor upgrades with increased functionality, but I really look forward to having a full .PDF of the new software documentation. The four existing volumes are simply excellent but are so in need of an update. Given Technical's track record I guess that we will be kept waiting but the waiting will be worthwhile for the end product! Can we anticipate any Christmas presents?
 

srnet

Senior Member
Wow, this shows how far Manual 2 is getting left behind from the on-line syntax guide; never having heard the "downto" syntax I searched the manuals in vain but there it is in the on-line guide, making me wonder what else I am missing. I know that we are probably waiting for some PICAXE editor upgrades with increased functionality, but I really look forward to having a full .PDF of the new software documentation. The four existing volumes are simply excellent but are so in need of an update. Given Technical's track record I guess that we will be kept waiting but the waiting will be worthwhile for the end product! Can we anticipate any Christmas presents?
Really ?

Manual 2, available from within the editor and without the need to be 'online" seems to have all the details;

for...next
Syntax:
FOR variable = start TO end {STEP {-}increment}
(other program lines)
NEXT {variable}
- Variable will be used as the loop counter
- Start is the initial value of variable
- End is the finish value of variable
- Increment is an optional value which overrides the default counter value of
+1.

If Increment is preceded by a ‘-’, it will be assumed that Start is greater
than End, and therefore increment will be subtracted (rather than added) on
each loop.
 

AllyCat

Senior Member
Hi,

I think Circuit was specifically referring to the DOWNTO command, which I must say is new to me as well. Of course there are very often several (or even many) different ways to construct a program, but are "rich" commands of this type realy useful? My first objection is that it's not in the "reserved words" list of the latest Manual 2. That means that a program which used to compile correctly may now fail in PE6, or that "new" programs may fail in PE5.

Certainly in an "educational" environment, one does not wish to "discourage" students, but is permitting (or even encouraging) "free" or "lazy" constructs the best solution? There was a recent discussion that the line separator (colon) is (sometimes) "optional". IMHO a better method is to give full and informative error messages.

If DOWNTO is such a "great" construct then why is there no UPTO (or is there)? Do any other languages use it? And is it really "right" (or useful to a student) that the (PE6) compiler accepts FOR b1 = 1 DOWNTO 10 without a warning, but only offers "Syntax error on line 1 at/before position 15" in response to a FOR b1 = 1 UPTO 10 ?

Cheers, Alan.
 

westaust55

Moderator
There are a few other cases of such gems as well. For example try:

Code:
FOR b0 = 0 TO 10
  b1 = b0//2
 IF b1 IS ON THEN
   SERTXD ("ON",cr,lf)
 ELSE
   SERTXD ("OFF",cr,lf)  
 ENDIF  
NEXT b0
 

oracacle

Senior Member
on a similar note to Inglewoodpete you could also

Code:
      [color=Purple]b10 [/color][color=DarkCyan]= [/color][color=Navy]40
      [/color][color=Blue]do
            [/color][color=Green]'do sutff here
            [/color][color=Blue]dec [/color][color=Purple]b10
            [/color][color=Blue]if [/color][color=Purple]b10 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then exit
      loop[/color]
I have used this quite a lot for exiting menu routines when multiple values had to be correct
 

Dippy

Moderator
That's why I asked if edmunds was talking about the For/Next loop, oracacle.

srnet's post shows exactly why the manual could do with a tickle at a convenient moment.
i.e. the information there but it's not obvious to the newbie. An example would be useful for the novice, especially as PICAXE is aimed at beginners.
A 4 line example would be easy to add.

And I'd never heard of DOWNTO either... I couldn't find it in the online Manual.
 

Circuit

Senior Member
Really ?

Manual 2, available from within the editor and without the need to be 'online" seems to have all the details;
Yes, as Allycat indicates, I think that you are missing the point; I was highlighting there appears to be a new reserved word here providing a new syntax that is not documented in the printed manual; DOWNTO. The fact that you have missed it is in itself interesting in that I think that the deviation from the "standard" form of BASIC was so unexpected that you quite understandably read right past it. Alan also makes a useful observation that it has not previously been flagged up as a reserved word and the logical derivative UPTO does not seem to exist.

And I'd never heard of DOWNTO either... I couldn't find it in the online Manual.
Dippy, it was my lack of clarity here when referring to the "on-line guide" - I meant the pull-down list of BASIC Commands that appears above in the header of this forum.

I rather think that this is an unwelcome addition to the PICAXE form of BASIC although when I think of a very young programmer getting his head around the term "STEP -1" I can perhaps see why "DOWNTO" is easier to understand. I guess that we put it in the same category as FORWARD and BACKWARD although these are labelled rather sensibly in the guide as "This command is not normally used outside of the classroom." Perhaps it would be helpful if DOWNTO had the same footnote.

Rather importantly though, I think that a revised list of Reserved Words should be published as soon as possible.
 

SAborn

Senior Member
I agree its time Techenical done an upgrade on commands in the manuals, and put the current tools back in the tool box for all to use.
Not to say there is a dozen ways you can achieve the same result as "downto", but there is many missing links that need to be addressed, ....for example "Time" there is no real direct link to time as is a function and not a command.

I spent time on a different forum pointing out how to use "Time" as its not in the manual or at least clearly.
 
Top