break(int n) and continue(int n)

Started by Rincewind, October 14, 2007, 12:40:22 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Rincewind

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.
Getting things done is vital.

SplinterGU

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 ...
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Sandman

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.
-- Sandman

SplinterGU

Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Rincewind

#4
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!
Getting things done is vital.

SplinterGU

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...
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

SplinterGU

#6
for example:

[code language="bennu"]
loop
    loop
        if (...)
            goto my_exit;                         
        end                 
    end
 
    //<more code>
end
:my_exit
//<more code>
[/code]
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Rincewind

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 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.
Getting things done is vital.

HaCkZJuaNN

I think it doesn't exist now, but splinter says maybe it would be better to add it than to add break(int n).

SplinterGU

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...


Download Lastest BennuGD Release: http://www.bennugd.org/node/2

izubiaurre


Rincewind

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.
Getting things done is vital.

Sandman

Half on topic: I like GOTO and labels, good plan.
-- Sandman

FreeYourMind

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 ?

SplinterGU

Download Lastest BennuGD Release: http://www.bennugd.org/node/2