forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Goto command within an IF statement

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
dangt
Newbie


Joined: 19 May 2009
Posts: 20

PostPosted: Mon Aug 17, 2009 4:28 pm    Post subject: Goto command within an IF statement Reply with quote

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
View user's profile Send private message
Aslan
Valued Contributor
Valued Contributor


Joined: 31 May 2001
Posts: 589
Location: Memphis, TN USA

PostPosted: Mon Aug 17, 2009 11:42 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Hooligan
VDS Developer
VDS Developer


Joined: 28 Oct 2003
Posts: 480
Location: California

PostPosted: Tue Aug 18, 2009 3:25 am    Post subject: Reply with quote

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
View user's profile Send private message
Garrett
Moderator Team


Joined: 04 Oct 2001
Posts: 2149
Location: A House

PostPosted: Tue Aug 18, 2009 6:53 am    Post subject: Reply with quote

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
View user's profile Send private message
DaveR
Valued Contributor
Valued Contributor


Joined: 03 Sep 2005
Posts: 413
Location: Australia

PostPosted: Tue Aug 18, 2009 8:39 am    Post subject: Re: Goto command within an IF statement Reply with quote

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
View user's profile Send private message
vdsalchemist
Admin Team


Joined: 23 Oct 2001
Posts: 1448
Location: Florida, USA

PostPosted: Tue Aug 18, 2009 3:13 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
arcray
Valued Contributor
Valued Contributor


Joined: 13 Jul 2001
Posts: 242
Location: Aude, France

PostPosted: Fri Aug 21, 2009 8:34 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
dangt
Newbie


Joined: 19 May 2009
Posts: 20

PostPosted: Mon Aug 24, 2009 7:35 pm    Post subject: Reply with quote

arcray, can you post a sample code that would fail?
Back to top
View user's profile Send private message
arcray
Valued Contributor
Valued Contributor


Joined: 13 Jul 2001
Posts: 242
Location: Aude, France

PostPosted: Tue Aug 25, 2009 4:51 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
uvedese
Contributor
Contributor


Joined: 21 Jan 2006
Posts: 169
Location: Spain

PostPosted: Tue Aug 25, 2009 8:16 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
arcray
Valued Contributor
Valued Contributor


Joined: 13 Jul 2001
Posts: 242
Location: Aude, France

PostPosted: Tue Aug 25, 2009 9:22 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Garrett
Moderator Team


Joined: 04 Oct 2001
Posts: 2149
Location: A House

PostPosted: Tue Aug 25, 2009 4:13 pm    Post subject: Reply with quote

As I said above.. GOTO is fine. Smile
_________________
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
Back to top
View user's profile Send private message
Skit3000
Admin Team


Joined: 11 May 2002
Posts: 2166
Location: The Netherlands

PostPosted: Tue Aug 25, 2009 7:14 pm    Post subject: Reply with quote

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. Smile
_________________
[ Add autocomplete functionality to your VDS IDE windows! ]
Voor Nederlandse beginners met VDS: bekijk ook eens deze tutorial!
Back to top
View user's profile Send private message
vdsalchemist
Admin Team


Joined: 23 Oct 2001
Posts: 1448
Location: Florida, USA

PostPosted: Tue Aug 25, 2009 9:33 pm    Post subject: Reply with quote

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. Wink
_________________
Home of

Give VDS a new purpose!
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group