for/next or do/loop

martinn

Member
Which is better coding and or lower load on the picaxe to create a timing loop. A for/next or do/loop (both a similar bytes).
Another appraoch could be a long pause with polled interrupt, but I was more interested in the for/next do/loop question

'for/next
for w0 = 1 to 6000 'combined with pause makes 10 min run time
pulsout 2, 10000 ' flash a led during running
pause 1000
if pin1 = 1 then pump_off ' if push switch is pressed again
next w0
goto main

OR
'do/loop
do
inc w0
pulsout 3, 10000 ' flash a led during running
pause 1000
if pin1 = 1 then pump_off ' if push switch is pressed again
loop until w0 = 6000 ' gives roughly 10 minute run
goto main
 

Technical

Technical Support
Staff member
Doesn't really matter, they are about the same in terms of processing, as the vast majority of the time you are just waiting doing very little within the pulsout and pause commands, so use which ever you prefer.
 

techElder

Well-known member
I think in the example you give, the DO/LOOP does a better job in telling me what the routine does.
 

Chavaquiah

Senior Member
From a compiler/interpreter point of view, a for/next is just a common loop with explicit start/increment/exit conditions. Using one or the other sometimes derives more from one's background in programming rather than coding best practises.

Personally, I think a for/next makes for more readable code when we know in advance how many iterations we'll need.
 

westaust55

Moderator
As others have mentioned, once time delays are brought into the equation, speed of individual instructions may become less relevant unless part of the communications protocol. Program space requirements, particularly for the "M" sereis PICAXE chips can be more relevant.

FOR...NEXT loops have been a part of the BASIC programming lanuage almost since its inception.
But some folks thought that along with GOTO comamnds reslting in "Spagetti" code - all interwoven, so the DO...LOOP was added in an endeavour to allow a (supposedly) more structured programming flow.

With respect to your code snippets (and yes, this is more about structure options and saving bytes than timing):

Do you need the LED to only blink briefly for 0.1 second every second (as is the case with your PULSOUT command? Thats quite a short interval
or is toggling on/off with say a second in each state acceptable?

Note that with a PAUSE 1000 (== 1 second delay) and then doing 6,000 loops that equates to 6000 seconds = 100 minutes and not 10 as your program comment suggests.

So you could alternatively have some code such as (the following is untested and bytes required unchecked):
Code:
DO
  INC w0
  TOGGLE 3
  PAUSE 1000
UNTIL w0 = 600 OR Pin1 = 1	; loop until time has expired or the "pump off" signal is detected
LOW 3	 ; make sure the LED is left in the off state
IF Pin1 = 0 THEN GOTO Main
:
:
Pump_Off:
or if you want to save a byte variable (if they are gettig short) and you can live with a 8.5 minute duration then a possibility is:
Code:
DO
  INC b0
  HIGH 3	
  PAUSE 1000
  LOW 3	
  PAUSE 1000
UNTIL b0 = 0 OR Pin1 = 1  ; if b0 has rolled over thru 255 to 0
IF Pin1 = 0 THEN GOTO Main
:
:
Pump_Off:
 
Last edited:
Top