Bennu Game Development

English Forums => Suggestions => Topic started by: Rincewind on October 14, 2007, 12:40:22 AM

Title: break(int n) and continue(int n)
Post by: Rincewind on October 14, 2007, 12:40:22 AM
Hello everyone. :)

First of all I am glad Juan didn't make this forum Spanish-only like the fenix.divsite.net forum, because that would totally fail to adress the international community. English is after all the default internet language.

I have a suggestion that I've been thinking of for some time now: A break(int <amount of loops>) and continue(int <amount of loops>) would be fantastic to have. Basically what they do is breaking or continuing multiple nested loops. I've missed this while programming and had to use variables to pass a signal between multiple planes of nested loops instead, which really isn't handy.

Hopefully you'll implement it, thanks in advance.
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on October 14, 2007, 01:50:34 AM
Hi! Wellcome!

I don´t understand, what can be the use for this sentences...

break and continue, not only for loop use... the condition should not be in break or continue, but in the while, for, etc ...
Title: Re: break(int n) and continue(int n)
Post by: Sandman on October 14, 2007, 02:18:41 AM
He means:
[code language="bennu"]
while( cond1 )
    while( cond2 )
        if( cond3 )
            cond1 = cond2 = false;
        end
    end
end
[/code]
...becomes...
[code language="bennu"]
while( cond1 )
    while( cond2 )
        if( cond3 )
            break( 2 );
        end
    end
end
[/code]
...I think.
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on October 14, 2007, 02:47:14 AM
Too complex...
Title: Re: break(int n) and continue(int n)
Post by: Rincewind on October 14, 2007, 01:13:53 PM
Quote from: JuanI don´t understand, what can be the use for this sentences...

break and continue, not only for loop use... the condition should not be in break or continue, but in the while, for, etc ...

The use of this is that you for example can break multiple loops, which can't be done by setting the conditions of two loops to true simply because the current loop cycles (the cycle of the inner and outer loop, in the example below) will still be finished then, that's why these loop-manipulative statements are handy in the first place.

[code language="bennu"]
loop
    loop
        if (...)
            My_variable=1;
            break;                       
        end               
    end
       
    if (My_variable)
        break;
    end
               
    //<more code>
end
[/code]

would become:

[code language="bennu"]
loop
    loop
        if (...)
            break(2);                       
        end               
    end
             
    //<more code>
end
[/code]

Notice how <more code> isn't executed in the example above. By simply setting conditions of loops to true (for example in For or While loops) instead of using break, <more code> will be executed which wasn't what we wanted!
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on October 14, 2007, 04:19:09 PM
Is very easy put bugs in your code with this method, for example, including a new loop in huge loop...
Maybe the best options is add a GOTO statement...
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on October 14, 2007, 04:20:56 PM
for example:

[code language="bennu"]
loop
    loop
        if (...)
            goto my_exit;                         
        end                 
    end
 
    //<more code>
end
:my_exit
//<more code>
[/code]
Title: Re: break(int n) and continue(int n)
Post by: Rincewind on October 14, 2007, 05:58:35 PM
QuoteIs very easy put bugs in your code with this method, for example, including a new loop in huge loop...
Maybe the best options is add a GOTO statement...

Well, the idea of structured programming (http://en.wikipedia.org/wiki/Structured_programming) using structured statements like break(int n) is that it would be less easy to slip in a bug than using GOTO's everywhere.  :-\

But I guess if a break(int n) is too complex to add then GOTO is indeed better than having an extra variable and an extra if statement. I didn't even realize a GOTO existed in Bennu/Fenix.
Title: Re: break(int n) and continue(int n)
Post by: HaCkZJuaNN on October 14, 2007, 07:15:28 PM
I think it doesn't exist now, but splinter says maybe it would be better to add it than to add break(int n).
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on October 14, 2007, 07:41:43 PM
GOTO never is buggy... is exact...

I never heard of "break(n)", only "break"...

GOTO don't exist yet... but could exist in the future...


Title: Re: break(int n) and continue(int n)
Post by: izubiaurre on October 14, 2007, 07:48:25 PM
Quote from: SplinterGU on October 14, 2007, 07:41:43 PM
I never heard of "break(n)", only "break"...

I haven't heard before too.
Title: Re: break(int n) and continue(int n)
Post by: Rincewind on October 14, 2007, 08:37:30 PM
Quote from: HaCkZJuaNNI think it doesn't exist now, but splinter says maybe it would be better to add it than to add break(int n).

Ah right.

Quote from: SplinterGUIGOTO never is buggy... is exact...

I didn't mean GOTO itself is buggy, but that using GOTOs as opposed to a structured statement will allow the programmer to get lost in his own code easier, and thus the programmer could introduce bugs easier. Of course it is up to the programmer what to use, but personally I'd prefer a break(n) statement. Optionally of course GOTO would also be an addition, since there isn't one yet.

QuoteI never heard of "break(n)", only "break"...

Break(n) already exists in some newer languages, for example PHP (http://nl3.php.net/manual/en/control-structures.break.php).
Title: Re: break(int n) and continue(int n)
Post by: Sandman on October 14, 2007, 09:06:47 PM
Half on topic: I like GOTO and labels, good plan.
Title: Re: break(int n) and continue(int n)
Post by: FreeYourMind on July 22, 2010, 11:28:44 PM
Quote from: SplinterGU on October 14, 2007, 04:20:56 PM
for example:

[code language="bennu"]
loop
    loop
        if (...)
            goto my_exit;                         
        end                 
    end
 
    //<more code>
end
:my_exit
//<more code>
[/code]

I have an error:


IF (_key(_space, _key_down))
GOTO etiqueta;
END

IF (_key(_space, _key_up))
    BREAK;
END

:etiqueta

....



Error: Unknown identifier: Etiqueta.

What's wrong ?
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on July 23, 2010, 06:20:40 AM
tal vez "etiqueta:" ?
Title: Re: break(int n) and continue(int n)
Post by: FreeYourMind on July 23, 2010, 07:14:29 AM
What means 'tal vez' ?
Hhehheheh, it's a joke ;)

etiqueta: it's the same, don't work.
An example please  :D
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on July 23, 2010, 07:32:14 AM
works...


import "mod_say";

begin

    say ( "Init" );

    if ( 1 )
        goto label;
    end

    say ( "I don't must displayed!" );

    return;

label:
    say ( "I'm label!" );

end


if you had made an example you had solved it!
Title: Re: break(int n) and continue(int n)
Post by: FreeYourMind on July 23, 2010, 08:14:14 AM
Don't work, don't recognize the label.


IF (_key(_space, _key_down))
     GOTO label;
END

IF (_key(_space, _key_up))
     BREAK;
END

label: say("lol");


>:( Some import, declaration is missing ?
Title: Re: break(int n) and continue(int n)
Post by: FreeYourMind on July 23, 2010, 08:26:20 AM
Your example works!!!!


Here is my example, don't work:


import "mod_say";
import "mod_key";

begin

    my_process();

LOOP

if (key(_esc))
    exit("");
END


FRAME;
END

end

PROCESS my_process()
BEGIN

say ( "Init" );

LOOP

  if (key(_space))
  GOTO label;
  END

  label: say("lol");

FRAME;
END

END

Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on July 23, 2010, 12:25:10 PM
label must be aligned to margin left! :P
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on July 23, 2010, 12:30:53 PM
works!


import "mod_video";
import "mod_say";
import "mod_key";
import "mod_proc";

begin

    my_process();

LOOP

if (key(_esc))
    exit("");
END


FRAME;
END

end

PROCESS my_process()
BEGIN

say ( "Init" );

LOOP

  if (key(_space))
  GOTO label;
  END

    frame;
continue;
label: say("lol");

FRAME;
END

END
Title: Re: break(int n) and continue(int n)
Post by: FreeYourMind on July 23, 2010, 12:52:35 PM
Interesting error origin... It's posible to fix and ignore tabulation when using goto's ?
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on July 23, 2010, 12:55:55 PM
It isn't an error... It's is intentional... must be...
Title: Re: break(int n) and continue(int n)
Post by: SplinterGU on July 23, 2010, 01:01:50 PM
In other way, in this thread we talk only about a proposal GOTO syntax, the final GOTO syntax thread is other... I'll close this thread.