goto within if loop question

TheChief

Senior Member
I am running into some difficulty with the structure of the below piece of code, I have simplified the below as much as I can as an example of what I am attempting in a slightly more complex scenario.

Basically what I want to happen is for eg. in this example below is for the "send" subroutine to be called sequentially in the first part of the IF statement and then exit the loop.
However when the "return" happens from the 1st "send" subroutine the IF statement seems to pick up at the start of the IF loop and then re-enters into the 1st "send" subroutine, this occurs indefinitely.

I hope I have explained my issue clearly enough and the below highlights what I am trying to achieve.


Code:
Symbol lastlocation = w1
Symbol nextlocation = w2
Symbol last = w3
Symbol x = w4
Symbol y = w5

wait 10
lastlocation = 5
nextlocation = 2

goto main

main:

sertxd ( "+++START MAIN+++",cr,lf)

if lastlocation >= nextlocation then
	last = 1
	sertxd ( "IF loop last ",#last,cr,lf)
	 x = lastlocation + 1
	 y = 240
	 goto send
	 last = 2
	 sertxd ( "IF loop last ",#last,cr,lf)
	 x = 1
	 y = lastlocation
	 goto send
else
	 last = 3
	 sertxd ( "IF loop last ",#last,cr,lf)
	 x = 1
	 y = nextlocation
	 goto send
	 last = 4
	 sertxd ( "IF loop last ",#last,cr,lf)
	 x = nextlocation
	 y = 240
	 goto send
		
endif

sertxd ( "+++END MAIN+++",cr,lf)

goto main

send:

sertxd ( "lastlocation ",#lastlocation, 5,"nextlocation ",#nextlocation,5,"last ",#last,cr,lf)

return

Output from terminal shows it repeats the first send loop continuously.


Code:
+++START MAIN+++
IF loop last 1
lastlocation 5nextlocation 2last 1
+++START MAIN+++
IF loop last 1
lastlocation 5nextlocation 2last 1
 

lbenson

Senior Member
Several errors compounded. Your "send" routine ends with a "return". If you enter it with "goto" and then return, you will get stack errors. "Return" must be matched with "gosub".

Also, there is no point in having any code following a "goto"--it won't be executed (unless it begins with a label which can be reached with another "goto"). Some of your problems can be fixed with "gosub send : goto main". I am not certain what you are trying to accomplish after your "goto" statements.
 

TheChief

Senior Member
That was really stupid of me, I don't believe that I did not spot the obvious "gosub" / "goto"
Thanks @lbenson

After modifying slightly now works as should, the goto and definition at the start and end is just to avoid looping through the setting of variables etc.

Code:
Symbol lastlocation = w1
Symbol nextlocation = w2
Symbol last = w3
Symbol x = w4
Symbol y = w5

wait 10
lastlocation = 5
nextlocation = 2

sertxd ( "=========",cr,lf)

goto main

main:

sertxd ( "+++START MAIN+++",cr,lf)

if lastlocation >= nextlocation then
	last = 1
	sertxd ( "IF loop last ",#last,cr,lf)
	 x = lastlocation + 1
	 y = 240
	 gosub send
	 last = 2
	 sertxd ( "IF loop last ",#last,cr,lf)
	 x = 1
	 y = lastlocation
	 gosub send
else
	 last = 3
	 sertxd ( "IF loop last ",#last,cr,lf)
	 x = 1
	 y = nextlocation
	 gosub send
	 last = 4
	 sertxd ( "IF loop last ",#last,cr,lf)
	 x = nextlocation
	 y = 240
	 gosub send
		
endif

sertxd ( "+++END MAIN+++",cr,lf)

goto main

send:

sertxd ( "lastlocation ",#lastlocation,15,"nextlocation ",#nextlocation,15,"last ",#last,cr,lf)
wait 2
return

Code:
=========
+++START MAIN+++
IF loop last 1
lastlocation 5nextlocation 2last 1
IF loop last 2
lastlocation 5nextlocation 2last 2
+++END MAIN+++
+++START MAIN+++
IF loop last 1
lastlocation 5nextlocation 2last 1
IF loop last 2
lastlocation 5nextlocation 2last 2
 

TheChief

Senior Member
Made the same mistake again during the week. A program wasn't working as expected eg. It did not go into the second loop. I currently do most of my work using the AXEpad on OSX, I did download the Windows Picaxe editor a few days ago and was surprised with all the sample code included. I suppose my question is: is there more functional testing of code included in the Win binary then the AXEpad versions?
Would it be worth my while running a program through the Windows editor as a validation process?
 

hippy

Ex-Staff (retired)
I suppose my question is: is there more functional testing of code included in the Win binary then the AXEpad versions?
Neither AXEpad or Programming Editor will detect program flaws of this kind, where the PICAXE is instructed to do something which is notionally valid but does not achieve the desired result.
 
Top