in reply to Re^2: A way to avoid repeated conditional loops
in thread A way to avoid repeated conditional loops

Why is your for loop idiom better than AR's or JavaFan's while loop statements above?

  • Comment on Re^3: A way to avoid repeated conditional loops

Replies are listed 'Best First'.
Re^4: A way to avoid repeated conditional loops
by ikegami (Patriarch) on Aug 24, 2011 at 20:36 UTC

    I already pointed out that AR's version is wrong. You can't rely on readline returning EOF or errors more than once.

    I thought mine was better than JavaFan's because it lead to the elimination of the duplicate "g()", but that turned out to be a mistake (as per my earlier update).

    I still prefer mine over JavaFan's, though. Since mine flows from top to bottom, it requires less skipping around to follow the code.

      Ah! Now I see what you're saying.

      Using AR's code, suppose the file is empty, then the very first while (<FILE>) will return EOF, then the code flows to the second while (<FILE>). There, even though the file pointer is at the end of the file, the <> operator won't return an EOF a second time. And the code in that second while loop will get executed when it should not.

      Thinking of it this way, I can now see how both yours and JavaFan's code avoid that trap. Thanks.