in reply to (tye)Re: The last command is like the break statement in C
in thread The last command is like the break statement in C

'C' most certainly does have 'next' loop flow modification! It's called 'continue'.
#include <stdio.h> main (int argc, char **argv) { int i; for (i = 0; i < 10; i++) { if (i == 5) continue; printf ("i = %d\n", i); } } [jcw@linux jcw]$ gcc t.c [jcw@linux jcw]$ ./a.out i = 0 i = 1 i = 2 i = 3 i = 4 i = 6 i = 7 i = 8 i = 9 [jcw@linux jcw]$
--Chris

e-mail jcwren
  • Comment on (jcwren) RE: (tye)Re: The last command is like the break statement in C
  • Download Code

Replies are listed 'Best First'.
(tye2)Re: The last command is like the break statement in C
by tye (Sage) on Sep 27, 2000 at 19:00 UTC

    Thanks. I forgot about "continue" in C, which I don't recall ever using. But this strengthens the story...

    Now that Perl has "redo" and "next", both of these "continue" the loop processing so they really both had to be named something other than "continue". Plus, Perl lets you write for(a;b;c) as a;while(b){}continue{c} (I hope I got that right). It makes sense to use "continue" here since that block is what is executed whenever you decide to continue looping. So having "continue" introduce that block as well as be a loop flow modifier would probably lead to more parsing confusion. So we had to rename "continue", we might as well rename "break" as well.

    I also wondered if "break" was being saved for when Perl adds a real "case" statement. I hate this in C:

    while( bool ) { switch( input ) { case END: break; } }
    What I really want to do is break out of the while loop. The work-arounds for this overloading of "break" in C are usually a bit ugly (depending on the specifics). So if Perl uses "break" for "case" statements, then I could code "last" above and get what I often want very easily.

            - tye (but my friends call me "Tye")
      Just to confuse C programmers, Perl has a continue keyword that is used in looping. If after your main loop block you have a block labelled continue, it will be executed between iterations. For giggles and grins, within a continue block the word next will restart the continue block. Combining continue, redo, and next properly you can write a triple-loop in one loop. I have no idea why someone would want to do this, but you can.