| View previous topic :: View next topic |
| Author |
Message |
dangt Newbie
Joined: 19 May 2009 Posts: 20
|
Posted: Mon Aug 17, 2009 4:28 pm Post subject: Goto command within an IF statement |
|
|
IF ()
...
GOTO a
ELSE
...
GOTO b
END
Will my code have issues if I put a GOTO command within an IF statement?
Since the END is never reached. Will I ever get a Command nested too deeply error because of this? |
|
| Back to top |
|
 |
Aslan Valued Contributor


Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Mon Aug 17, 2009 11:42 pm Post subject: |
|
|
I know it used to but, I'm not sure any more. I tend to use GOSUB more often or define a custom function than use GOTO anymore.
Although, I can say I haven't experience any issues using GOTO in a timer loop.
| Code: | :Timer
%E= @event()
If %E
GOTO %E
else
%X = "Do something else!"
end |
|
|
| Back to top |
|
 |
Hooligan VDS Developer


Joined: 28 Oct 2003 Posts: 480 Location: California
|
Posted: Tue Aug 18, 2009 3:25 am Post subject: |
|
|
The easiest way around that is
| Code: |
If xxx
%g = a
else
%g = b
end
goto %g
|
Hooligan _________________ Hooligan
Why be normal? |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Tue Aug 18, 2009 6:53 am Post subject: |
|
|
No, there's no problem doing that. _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
DaveR Valued Contributor


Joined: 03 Sep 2005 Posts: 413 Location: Australia
|
Posted: Tue Aug 18, 2009 8:39 am Post subject: Re: Goto command within an IF statement |
|
|
| dangt wrote: | Will my code have issues if I put a GOTO command within an IF statement?
Since the END is never reached. Will I ever get a Command nested too deeply error because of this? |
Usually that error only occurs if something has gone seriously wrong - usually a typo somewhere, or a missing end within a loop. _________________ cheers
Dave |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Tue Aug 18, 2009 3:13 pm Post subject: |
|
|
Other than it not being good form I see no issue with putting a goto in the middle of a if statement. _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
arcray Valued Contributor


Joined: 13 Jul 2001 Posts: 242 Location: Aude, France
|
Posted: Fri Aug 21, 2009 8:34 am Post subject: |
|
|
Eventually you will reach a too many nested loops error; always use the code listed above, or below:
| Code: | If xxx
%g = a
else
%g = b
end
goto %g |
_________________ Andrew GRAY
If you don't know I am looking for work, I won't get the job.
andrewrcgray.com |
|
| Back to top |
|
 |
dangt Newbie
Joined: 19 May 2009 Posts: 20
|
Posted: Mon Aug 24, 2009 7:35 pm Post subject: |
|
|
| arcray, can you post a sample code that would fail? |
|
| Back to top |
|
 |
arcray Valued Contributor


Joined: 13 Jul 2001 Posts: 242 Location: Aude, France
|
Posted: Tue Aug 25, 2009 4:51 am Post subject: |
|
|
Looks as if GOTOs are fine, but GOSUBs cause an error 10
| Code: |
%A = 1stLoop
%B = 2ndLoop
%X = 0
OPTION ERRORTRAP,ERRORTRAP
:EvLoop
IF %A
GOSUB %A
ELSE
GOSUB %B
END
:1stLoop
%A =
%X = @SUCC(%X)
GOTO EvLoop
:2ndLoop
%A = 1stLoop
%X = @SUCC(%X)
GOTO EvLoop
:ERRORTRAP
INFO There have been %X iterations before an ERROR @ERROR(E) at Line number @ERROR(N)
EXIT
EXIT
|
_________________ Andrew GRAY
If you don't know I am looking for work, I won't get the job.
andrewrcgray.com |
|
| Back to top |
|
 |
uvedese Contributor


Joined: 21 Jan 2006 Posts: 169 Location: Spain
|
Posted: Tue Aug 25, 2009 8:16 am Post subject: |
|
|
Hi arcray:
A "GOSUB" call needs a corresponding "EXIT" statement. It gives an error because you call a subroutine more than 16 times.
VDS6 documentation:
| Quote: | | "GOSUB commands may be nested a maximum of 16 levels deep" |
__________________
uVeDeSe
http://www.uvedese.es
__________________ |
|
| Back to top |
|
 |
arcray Valued Contributor


Joined: 13 Jul 2001 Posts: 242 Location: Aude, France
|
Posted: Tue Aug 25, 2009 9:22 am Post subject: |
|
|
Yes, I have been avoiding doing it for so long, I forgot how to make the erroneous code... _________________ Andrew GRAY
If you don't know I am looking for work, I won't get the job.
andrewrcgray.com |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Tue Aug 25, 2009 4:13 pm Post subject: |
|
|
As I said above.. GOTO is fine.  _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
Skit3000 Admin Team

Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
Posted: Tue Aug 25, 2009 7:14 pm Post subject: |
|
|
The VDS interpreter probably counts the number of IF/END statements (for IF it does +1, for END -1). When it encounters a GOTO command, I think it will reset the nested-count-variable back to zero, so that would be why it doesn't cause any errors.  _________________ [ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial! |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Tue Aug 25, 2009 9:33 pm Post subject: |
|
|
Your probably correct Skit however I would think of the goto as a break/jump statement. It breaks the current code execution and jumps to the label or line number specified just like the old DOS basic languages used too. I would say that this is ok as long as you don't keep re-entering the same code since this would run a risk of an endless loop. One should focus on proper structure as much as possible. If you do you will find that your code will perform better and will be easier to maintain.  _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
|