Posted: 25th Jun 2007 1:38
I seem to remember there being a command for jumping back to the start of a loop mid-loop.

I thought it was something like START or CONTINUE but I can't find it in the help file.

I may be mixing this up with PHP which does have a [url=http://us2.php.net/continue]continue command.

Is there such a thing in DBP?
Posted: 25th Jun 2007 1:52
The goto command.
+ Code Snippet
top:
do
   goto top
loop
Posted: 25th Jun 2007 1:57
That is wrong Sasuke The goto command cannot jump back in the code, only forward. The gosub command however will do the trick:
+ Code Snippet
myLabel:

print "You have jumped back!"
wait key
cls

do

if spacekey() = 1
  gosub myLabel
endif

loop



If that is what you where looking for Mistrel

You would also be able to use them as sub-routines:
+ Code Snippet
do

if keystate(200) = 1
   gosub myRoutine
endif

loop

myRoutine:

   something

return

Posted: 25th Jun 2007 2:00
There's a continue command in C++... I'll see it I can find something like it in DBP and get back to ya

[edit]
Nop, cant find it.

[edit]
Sasuke, "goto" is a bad habbit
Posted: 25th Jun 2007 2:03
I never use goto's because I despise them, so go with Nerd's idea but goto still works.

+ Code Snippet
myLabel:

print "You have jumped back! Using goto"
wait key
cls

do

if spacekey() = 1
  goto myLabel
endif

loop
Posted: 25th Jun 2007 2:04
Fair enough, it does actually work. I just always thought goto only worked the other way around. My bad
Posted: 25th Jun 2007 2:05
Funny, I thought that for gosub.
Posted: 25th Jun 2007 2:13
here's another way

+ Code Snippet
For i = 1 to 10
rem your looping code here
rem check to see if you need to go back to the beginning
If <your condition> = <your test value>  
  i = 0
Endif
next i
Posted: 25th Jun 2007 2:15
As already stated, Goto will let you jump to a label at the start of your main program loop, but the fact is that using Goto should be avoided at all costs. It's the first step on a slippery slope to spaghetti code.

What's more, if you are having to think about jumping to the start of a loop half way though it, then to be honest your program isn't laid out correctly.

Take a look at tutorial 2 here:

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

TDK_Man
Posted: 25th Jun 2007 2:17
I'm sad to hear that DBP doesn't have any form of a CONTINUE but I've been able to find a solution using a while loop which worked out better.

Thank you for your suggestions.
Posted: 25th Jun 2007 4:51
Sasuke, "goto" is a bad habbit

Not really, at least when it comes to workarounds for a lack things like the continue command - besides, continue does more or less what goto does, only you have to create your own tag with the latter. Another use I find for it is to jump case labels. I find goto is more reserved for the more advanced coders though, the inexperienced run into problems with it due to using it in place of proper code structure.

What's more, if you are having to think about jumping to the start of a loop half way though it, then to be honest your program isn't laid out correctly.

Tell that to the guy who thought up the continue keyword for C. I personally use it a fair bit.
Posted: 25th Jun 2007 4:56
Wow Benjamin it's late/early in France (4am)

I have to agree with Sasuke, gotos and jumping out of loops shows lack of program design knowledge.

If you realy must jump of of a loop set the condition to end the loop eg.

for i = 1 to 1000000

// do somthing while in this loop
print "Counting please wait! " + str$(i)
sync

//but if an event happens an you want to exit
if spacekey() =1 then i = 1000000

next i
Posted: 25th Jun 2007 5:04
Wow Benjamin it's late/early in France (4am)

Indeed, I'm quite a nocturnal cat though.

I have to agree with Sasuke, gotos and jumping out of loops shows lack of program design knowledge.

It depends on the loop. Jumping out of loops that perform a specific operation is very common, which is why a keyword exists in (I'm guessing) just about every language that uses control structures, to break out of the loop. Jumping out of the main loop, however, is not so clever.

If you realy must jump of of a loop set the condition to end the loop eg.

I'm sure you realise that exit exits the loop?

So many misconceptions people have...
Posted: 25th Jun 2007 7:14
Gatorhex:
See my code snippet above on how to go back to the top of the loop.

It depends on the loop. Jumping out of loops that perform a specific operation is very common, which is why a keyword exists in (I'm guessing) just about every language that uses control structures, to break out of the loop. Jumping out of the main loop, however, is not so clever.

Benjamin:
I agree with you 110 %. Where is it written programs can and should only go forwards? I jump back to beginning of loops all the time. Makes for nice, tight nested loops.
Posted: 25th Jun 2007 7:16
I guess it's my Uni education. You cannot document spagetti code and they made me document everything I wrote

When you work on commerical code it's a nightmare if you have to pick up an amatur coders piece of work.

I can't remember does C++ have goto/gosubs? I don't think it do.
Posted: 25th Jun 2007 10:55
Nerd, your Gosub code is absolutely the wrong thing to do! You will end up killing the stack with an unlimited number of Gosubs and no returns. Every time you Gosub, the program has to add a reference on the stack to where it should return.
Posted: 25th Jun 2007 11:05
@BatVink:

Which one of the codes? I have two examples up there with a gosub example, where I would never do something as the first code snippet I showed. Although if you mean this one:
+ Code Snippet
do

if keystate(200) = 1
   gosub myRoutine
endif

loop

myRoutine:

   something

return


I'm not able to see what's wrong, as that's how I've always used it without any problems in bigger projects.

I'm sorry if I've misunderstood something. I've been up all night and it's now 10 in the morning here hehe. So I'm a bit tired.
Posted: 25th Jun 2007 15:45
@GatorHex, C does have a goto command.

@The Nerd, He meant the first piece of code.

I'm with Benjamin on the use of goto - when no other command is available, and when used with care, goto is ok to use.

In this case, I might use an 'if' unless it made the code difficult to read.
Posted: 25th Jun 2007 18:45
I read the first post of this thread and figured MISTREL was talking about a Do/Loop..

The main difference with a GOTO v's GOSUB is a GOTO only needs a LABEL where a GOSUB needs a LABEL and a RETURN.
GOTO Code...
+ Code Snippet
Do
 Top:
 Cls
 If T=Timer() Then T=Timer()+1000: Jump = Not Jump
 If Jump Goto Top
 Text 0,0,Str$(Timer())
Loop

GOSUB Code...
+ Code Snippet
Do
 Cls
 If T=Timer() Then T=Timer()+1000: Jump = Not Jump
 If Jump Gosub Top
 Text 0,0,Str$(Timer())
Loop
End
Top:
`Nothing Here
Return


Running these examples will show why the GOTO command is best suited for this application. The GOSUB command when it returns, continues from where it GOSUB'd.

In saying that I'd opt for using The CASE SELECT to eliminate code sections I don't want running this LOOP.
CASE Code...
+ Code Snippet
Do
 Cls
 If T=Timer() Then T=Timer()+1000: Jump = Not Jump
 Select Jump 
  Case > 0
   Text 0,0,Str$(Timer())
  EndCase
 EndSelect
Loop

if on the other hand you widshed to exit the Do/Loop you could just use EXIT instead of GOTO..