For...Next in reverse

Euanw

New Member
Hi,

Do For Next loops work in reverse as well? i.e.

For b1 = 1 to 1000
Next b1

or

For b1 = 1000 to 1
Next b1

I have tried and it doesn't seem to work.

Regards

Euan
 

BeanieBots

Moderator
As stated by papaof2, use step -1, (or whatever step size you want.
The other reason your example won't work is because you trying to assign a value larger than 255 to a byte variable, b0.
If you want 1000 to 1, then you must use a word.

For w0=1000 to 1 step -1
..whatever
next w0

 

manuka

Senior Member
This is a early 1960s B.A.S.I.C. fundamental that even pre teen Bill Gates had to recognise.

Normal climbing value FOR...NEXT loops work without a STEP mention, as STEP just defaults to 1 with an incrementing loop. Non unity or decreasing value loops must however be STEP defined -
* FOR b1= 0 to 20 STEP 2 'increments even numbers

* FOR b2=31 to 1 STEP -1 'counts down by 1

* For b3=10 to 100 STEP 10 'increments by 10
 

tarzan

Senior Member
There are other possibilities with “for next” loops. Just to name a few see code below.

<code><pre><font size=2 face='Courier'> #picaxe 08M
#terminal 9600

symbol LS_Nibble = b2
symbol MS_Nibble = b3

let b6 = 2

setfreq m8

for b0 = &quot;z&quot; to &quot;a&quot; step - b6
sertxd (b0)
'pause 100
next

sertxd (CR,LF)
for b0 = $FF to $00 step - b6
gosub convert
sertxd (&quot;Hex = $&quot;,MS_Nibble,LS_Nibble,CR,LF)
'pause 100
next

for b0 = &quot;A&quot; to &quot;Z&quot;
sertxd (b0)
'pause 100
next

sertxd (CR,LF)
for b0 = %11111111 to %00000000 step - %00000010
sertxd (&quot;Bin = %&quot;,#bit7,#bit6,#bit5,#bit4,#bit3,#bit2,#bit1,#bit0,CR,LF)
'pause 100
next

end

convert:

let LS_Nibble = b0 &amp; %00001111
let MS_Nibble = b0 &amp; %11110000 / 16

lookup LS_Nibble,(&quot;0123456789ABCDEF&quot;),LS_Nibble
lookup MS_Nibble,(&quot;0123456789ABCDEF&quot;),MS_Nibble

return </font></pre></code>
 
Top