www.digitalmars.com         C & C++   DMDScript  

D - break from labed statement.

reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
to continue (a little) the old goto debate,
one of the uses of goto is to get to the end of a block from an if without
writing a 1000 nexted if else's ........

I'd like to be able to write
    done:{
        ....
        if (...) {
            if ( ... ) { break done; }
        }
    }

instead of writing
    done:do{
        ....
        if (...) {
            if ( ... ) { break done; }
        }
    } while( false );
Aug 21 2003
next sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
Named break and goto are effectively the same thing, right?  Labels attach
to the beginning or end of their block?  I'd rather them attach exactly at
the point of flow where the label is encountered, so you can put the label
either before or after a block.  Simpler than trying to identify the labels
with a loop or block.

Maybe if we removed the goto keyword while in the meantime extending the
power of the named break to effectively replace it... it would satiate the
whiners, a little bit.  ;)

Sean

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bi1t45$227t$1 digitaldaemon.com...
 to continue (a little) the old goto debate,
 one of the uses of goto is to get to the end of a block from an if without
 writing a 1000 nexted if else's ........

 I'd like to be able to write
     done:{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     }

 instead of writing
     done:do{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     } while( false );

Aug 21 2003
parent reply "Philippe Mori" <philippe_mori hotmail.com> writes:
"Sean L. Palmer" <palmer.sean verizon.net> a écrit dans le message de
news:bi1uqh$24u2$1 digitaldaemon.com...
 Named break and goto are effectively the same thing, right?  Labels attach
 to the beginning or end of their block?  I'd rather them attach exactly at
 the point of flow where the label is encountered, so you can put the label
 either before or after a block.  Simpler than trying to identify the

 with a loop or block.

 Maybe if we removed the goto keyword while in the meantime extending the
 power of the named break to effectively replace it... it would satiate the
 whiners, a little bit.  ;)

 Sean

 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bi1t45$227t$1 digitaldaemon.com...
 to continue (a little) the old goto debate,
 one of the uses of goto is to get to the end of a block from an if


 writing a 1000 nexted if else's ........

 I'd like to be able to write
     done:{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     }

 instead of writing
     done:do{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     } while( false );


I think that labelled break and continue should only allows a target that instruction that already support break or continue. If we need more power (i.e. arbitrary target) we can (and should uses goto). That way, break would always mean that we break a loop (or a switch) and continue that we continue a loop (or maybe a case if continue is used for fall-through). So for example, it would not be possible to do an infinite loop like above using break...
Aug 21 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Philippe Mori" <philippe_mori hotmail.com> wrote in message
news:bi2hn3$2vd3$1 digitaldaemon.com...
 "Sean L. Palmer" <palmer.sean verizon.net> a écrit dans le message de
 news:bi1uqh$24u2$1 digitaldaemon.com...
 Named break and goto are effectively the same thing, right?  Labels


 to the beginning or end of their block?  I'd rather them attach exactly


 the point of flow where the label is encountered, so you can put the


 either before or after a block.  Simpler than trying to identify the

 with a loop or block.

 Maybe if we removed the goto keyword while in the meantime extending the
 power of the named break to effectively replace it... it would satiate


 whiners, a little bit.  ;)

 Sean

 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bi1t45$227t$1 digitaldaemon.com...
 to continue (a little) the old goto debate,
 one of the uses of goto is to get to the end of a block from an if


 writing a 1000 nexted if else's ........

 I'd like to be able to write
     done:{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     }

 instead of writing
     done:do{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     } while( false );


like above using break...

read the code .... `} while( false);` its only every run once, no infinite loop, its just a do..while loop to allow break to break from it instead of goto to the statement just after the loop end (break breaks from loop it does not goto label)
Aug 21 2003
next sibling parent reply "Philippe Mori" <philippe_mori hotmail.com> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> a écrit dans le message de
news:bi3ngf$1nn4$2 digitaldaemon.com...
 "Philippe Mori" <philippe_mori hotmail.com> wrote in message
 news:bi2hn3$2vd3$1 digitaldaemon.com...
 "Sean L. Palmer" <palmer.sean verizon.net> a écrit dans le message de
 news:bi1uqh$24u2$1 digitaldaemon.com...
 Named break and goto are effectively the same thing, right?  Labels


 to the beginning or end of their block?  I'd rather them attach



 at
 the point of flow where the label is encountered, so you can put the


 either before or after a block.  Simpler than trying to identify the

 with a loop or block.

 Maybe if we removed the goto keyword while in the meantime extending



 power of the named break to effectively replace it... it would satiate


 whiners, a little bit.  ;)

 Sean

 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bi1t45$227t$1 digitaldaemon.com...
 to continue (a little) the old goto debate,
 one of the uses of goto is to get to the end of a block from an if


 writing a 1000 nexted if else's ........

 I'd like to be able to write
     done:{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     }

 instead of writing
     done:do{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     } while( false );


like above using break...

read the code .... `} while( false);` its only every run once, no infinite loop, its just a do..while loop to allow break to break from it instead of goto to the statement just after the loop end (break breaks

 loop it does not goto label)

The original code has a while but not the suggested remplacement... but not I see that break would mean goto after labelled instruction while continue would be generally equivalent to goto except that continue will not do the initialisation part or a for instruction. I haven't noticed before that the meaning was different that with a goto in such a case... so effectivelly they could be usefull on occasion as they would better document the code... void f(int a) { ext_loop : for (int i = 0; i < 10; ++i) { while (1) { if (a == 0) break ext_loop; // 1 if (a == 1) goto ext_loop; // 2 if (a == 2) continue ext_loop; // 3 } } } would essentially be equivalent to : void f(int a) { { ext_loop_a : int i = 0; ext_loop_b : for ( ; i < 10; ++i) { while (1) { if (a == 0) goto ext_loop_c; // 1 if (a == 1) goto ext_loop_a; // 2 if (a == 2) goto ext_loop_b; // 3 } } ext_loop_c: } } I know, f will loop forever if a is 2...
Aug 21 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
That's certainly interesting!

Sean

"Philippe Mori" <philippe_mori hotmail.com> wrote in message
news:bi4175$2662$1 digitaldaemon.com...
 The original code has a while but not the suggested remplacement...
 but not I see that break would mean goto after labelled instruction
 while continue would be generally equivalent to goto except that
 continue will not do the initialisation part or a for instruction.

 I haven't noticed before that the meaning was different that with a
 goto in such a case... so effectivelly they could be usefull on
 occasion as they would better document the code...

 void f(int a)
 {
     ext_loop : for (int i = 0; i < 10; ++i)
     {
         while (1)
         {
             if (a == 0) break ext_loop;    // 1
             if (a == 1) goto ext_loop;    // 2
             if (a == 2) continue ext_loop;    // 3
         }
     }
 }

 would essentially be equivalent to :

 void f(int a)
 {
     {
         ext_loop_a :
         int i = 0;

         ext_loop_b :
         for ( ; i < 10; ++i)
         {
             while (1)
             {
                 if (a == 0) goto ext_loop_c;    // 1
                 if (a == 1) goto ext_loop_a;    // 2
                 if (a == 2) goto ext_loop_b;    // 3
             }
         }
         ext_loop_c:
     }
 }

 I know, f will loop forever if a is 2...

Aug 22 2003
prev sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bi3ngf$1nn4$2 digitaldaemon.com...
 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bi1t45$227t$1 digitaldaemon.com...
 to continue (a little) the old goto debate,
 one of the uses of goto is to get to the end of a block from an if


 writing a 1000 nexted if else's ........

 I'd like to be able to write
     done:{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     }



its only every run once, no infinite loop, its just a do..while loop to allow break to break from it instead of goto to the statement just after the loop end (break breaks

 loop it does not goto label)

I don't see what's so superior about the above versus this: { .... if (...) { if ( ... ) { goto done; } } } done: Yes, you can misuse goto. But you can misuse the hell out named break if you tried to. ;) or HAD to. 8) Sean
Aug 22 2003
parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Sean L. Palmer" <palmer.sean verizon.net> wrote in message
news:bi4io8$31ck$1 digitaldaemon.com...
 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bi3ngf$1nn4$2 digitaldaemon.com...
 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bi1t45$227t$1 digitaldaemon.com...
 to continue (a little) the old goto debate,
 one of the uses of goto is to get to the end of a block from an if


 writing a 1000 nexted if else's ........

 I'd like to be able to write
     done:{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     }




I don't see what's so superior about the above versus this: { .... if (...) { if ( ... ) { goto done; } } } done:

not a great deal, only that 'goto' contains no implicit direction break implied down the code and out the loop, and when cutting and pasting, if you mode code outside the `done` loop you get an error.
 Yes, you can misuse goto.  But you can misuse the hell out named break if
 you tried to.  ;)   or HAD to.  8)

are not within.
Aug 22 2003
prev sibling parent "Vathix" <vathix dprogramming.com> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bi1t45$227t$1 digitaldaemon.com...
 to continue (a little) the old goto debate,
 one of the uses of goto is to get to the end of a block from an if without
 writing a 1000 nexted if else's ........

 I'd like to be able to write
     done:{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     }

 instead of writing
     done:do{
         ....
         if (...) {
             if ( ... ) { break done; }
         }
     } while( false );

I think it'd be better called "break from block to label's scope". If you can do it for any statement, what about mylabel: break; is that statement just skipped or does break break out of something else? This sounds like a good idea, although it doesn't exactly replace goto. I tend to think those loop tricks are more of a hack than using goto, this seems like a pretty good compromise.
Aug 21 2003