Author |
Topic: goto [loop] (Read 608 times) |
|
joker
Global Moderator
member is offline
Gender:
Posts: 157
|
|
goto [loop]
« Thread started on: Oct 31st, 2015, 8:04pm » |
|
I've seen this several times and am somewhat confused (not so hard to believe!)
Code:
[loop]
wait
[someOtherCode]
' blah
' blah
' blah
goto [loop]
The other way I've seen this is to replace the "goto [loop]" with "wait". There could be many labeled procedures with this at the end.
Is there a significant difference in the two methods? Is "wait" the same "wait" no matter where it is in the code?
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: goto [loop]
« Reply #1 on: Oct 31st, 2015, 8:11pm » |
|
on Oct 31st, 2015, 8:04pm, pnlawrence wrote:I've seen this several times and am somewhat confused (not so hard to believe!) |
|
Me too. It stems from a misunderstanding, I expect. It makes much more sense (to me) to replace the goto [loop] with another wait.
There is a bug in LB 4 which (in obscure circumstances) can cause execution to continue 'through' the wait (in which case the two options would behave differently), but it shouldn't happen, and never does in LBB.
Richard.
|
|
|
|
SarmedNafi
Junior Member
member is offline
Posts: 93
|
|
Re: goto [loop]
« Reply #2 on: Nov 1st, 2015, 01:56am » |
|
Well, I think Richard points to another problem.
The example code could be useful if my routines make "SET", the "RESET" routine should be between [loop] and wait. We will use one "reset" routine for all other "set" routines.
Richards answer related to the problem of TIMER in Liberty Basic.
Sarmed
|
« Last Edit: Nov 1st, 2015, 01:16am by SarmedNafi » |
Logged
|
|
|
|
SarmedNafi
Junior Member
member is offline
Posts: 93
|
|
Re: goto [loop]
« Reply #3 on: Nov 1st, 2015, 01:09am » |
|
In Liberty Basic because timer is broken, we have to use the posted example to get raid of the absence of a good TIMER. Or the only other choice is to use LBB with correct TIMER. If Carl fix the TIMER, Liberty Basic will work in 95% of it's abilities, the remaining 5% less is for the other bugs not fixed.
Sarmed
|
« Last Edit: Nov 1st, 2015, 01:21am by SarmedNafi » |
Logged
|
|
|
|
Rod
Full Member
member is offline
Gender:
Posts: 110
|
|
Re: goto [loop]
« Reply #4 on: Nov 1st, 2015, 07:20am » |
|
To answer the initial question. A program is either a static sequence of code blocks [labels] or Subs that will be called by events like a button press or mouse click or the timer firing. In which case the code jumps to the code block and executes to the wait statement. It then sits idle till the next event. Or the program loops and runs specific sequences of code blocks in a loop. This is when go to [loop] is most often seen. The loop can also be repeated by using a timer statement.
So the difference is all about exactly what program flow you need, looping or static or indeed combinations where you have a core loop and static code blocks that are called occasionally.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: goto [loop]
« Reply #5 on: Nov 1st, 2015, 07:25am » |
|
on Nov 1st, 2015, 01:09am, SarmedNafi wrote:In Liberty Basic because timer is broken, we have to use the posted example to get raid of the absence of a good TIMER. |
|
No, it makes no difference. In the code posted by the OP, goto [loop] has the same effect as wait; irrespective of whether you are using TIMER (in LB or LBB).
Quote:Richards answer related to the problem of TIMER in Liberty Basic. |
|
No it didn't! The bug related to TIMER in JB/LB is that you cannot use a SUB handler, but that's a completely separate issue from the one raised by the OP, and is unaffected by it.
At the risk of adding more confusion, there is another bug in LB which means you occasionally need to use this code instead of wait, but again it's a separate issue:
Code:[loop]
calldll #kernel32, "Sleep", 1 as long, r as long
scan
goto [loop] Richard.
|
|
|
|
Rod
Full Member
member is offline
Gender:
Posts: 110
|
|
Re: goto [loop]
« Reply #6 on: Nov 1st, 2015, 08:20am » |
|
Also in the dim and distant past Liberty needed that form of go to loop , wait. So if you are looking at very old or original code examples you will see that format. But the wait statement has been improved. So you can wait anywhere in a program and be whisked off to the next event.
|
|
Logged
|
|
|
|
joker
Global Moderator
member is offline
Gender:
Posts: 157
|
|
Re: goto [loop]
« Reply #7 on: Nov 1st, 2015, 08:54am » |
|
Quote:Is there a significant difference in the two methods? Is "wait" the same "wait" no matter where it is in the code? |
|
Ok, most everyone will / does understand how a GOTO works, so that isn't part of my confusion. Perhaps Rod hit on the reason that I've been seeing it done, as I've been doing a lot of reading (mostly in archives, but what else is there?) trying to wrap my brain around GUI programming.
I appreciate the comments. Good to know those workarounds for known bugs in LB, too.
I'm getting from all this that wherever you put the wait command, the program is essentially going back into the same loop "under the hood", but by adding a goto one will actually be adding code to get to that "under the hood" loop.
That's an easy decision for me to make.
|
|
Logged
|
|
|
|
SarmedNafi
Junior Member
member is offline
Posts: 93
|
|
Re: goto [loop]
« Reply #8 on: Nov 1st, 2015, 3:34pm » |
|
Quote:[loop] calldll #kernel32, "Sleep", 1 as long, r as long scan goto [loop]
|
|
Actually LB TIMER is a good delay element, we don't need to use SLEEP. The only example shipped with LB is "timerexample.bas", in this example TIMER act as a delay element, the next (tick) is done by the loop not by the TIMER. So I said what am I said, we have not to use (for next loop) but we must use GOTO [LOOP] for contuse running.
Code:
print "let's wait while looping"
for x = 1 to 10
print "Now at "; x;". Waiting...";
call waitMilliseconds 1000
print "done waiting!"
next x
end
sub waitMilliseconds ms
timer ms, [stopWaiting]
wait
[stopWaiting]
timer 0
end sub
The upper is the official example of TIMER, I said to my self if I want my program to work correctly I must limited my TIMER usage with this example. (it is a delay element)
Sarmed
|
« Last Edit: Nov 1st, 2015, 3:36pm by SarmedNafi » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: goto [loop]
« Reply #9 on: Nov 1st, 2015, 5:15pm » |
|
on Nov 1st, 2015, 3:34pm, SarmedNafi wrote:Actually LB TIMER is a good delay element, we don't need to use SLEEP. |
|
There are a number of reasons why Sleep is sometimes necessary:
For short delays. Although the TIMER parameter is specified in milliseconds, the shortest delay you can actually achieve is about 10 milliseconds and can be as long as 20 milliseconds:
Code: start = time$("ms")
timer 1, [pause]
wait
[pause]
finish = time$("ms")
timer 0
print "Delay was "; finish-start; " milliseconds"
end
To prevent an 'infinite loop' hogging the CPU. If your program is looping 'doing nothing' it will slow down other processes and can make the PC unresponsive. You should include a Sleep(0) or Sleep(1) in such loops:
Code: calldll #kernel32, "Sleep", 0 as long, r as long
In LB 4 (but not LBB) certain Windows API calls (e.g. the WMLiberty DLL) are incompatible with the WAIT statement, so the replacement code I listed must be used instead (this is not a delay routine):
Code:' replacement for WAIT
[loop]
calldll #kernel32, "Sleep", 1 as long, r as long
scan
goto [loop]
In LB 4 (but not LBB) there is a bug which causes TIMER to break the PROMPT statement, so it may be necessary to use Sleep instead. See this page at the LB Bug Tracker wiki. Richard.
|
|
|
|
SarmedNafi
Junior Member
member is offline
Posts: 93
|
|
Re: goto [loop]
« Reply #11 on: Nov 1st, 2015, 11:57pm » |
|
Thank you Richard for the wider scope explanation. Thank you Alyce for being here.
However any LB user soon or later will find himself wants to use (for example) keyboard pressing response program, he starts thinking of TIMER then he shocked with the unreliable performance of Liberty Basic, unless Carl will fix it, or he will jump to the Booster LBB (that what I did).
Regards,
|
« Last Edit: Nov 2nd, 2015, 12:08am by SarmedNafi » |
Logged
|
|
|
|
|