in reply to Use of SWITCH vs IF ELSE in speed and readability

bradcathey, your fist piece of code is perfectly alright, the second one is an emulated switch statement and the third one is wrong. It would have to be if ($_ == 1). Just execute the following code and you'll see it:
$x = 3; SWITCH: for ($x) { if (1) { print '1'; last SWITCH; }; if (2) { print '2'; last SWITCH; }; if (3) { print '3'; last SWITCH; }; print 'caught'; }
It's going to print 1, because 1 is true, so that the if (1) statement is always executed.
Hope this helped.
CombatSquirrel.
Entropy is the tendency of everything going to hell.

Replies are listed 'Best First'.
Re: Re: Use of SWITCH vs IF ELSE in speed and readability
by bradcathey (Prior) on Aug 30, 2003 at 17:35 UTC
    Thanks CombatSquirrel, your answers are always helpful, and innovative.

    So, there is nothing wrong with using the if/else structure? Just don't know why the Perl book says "horrors!"
      You know, TIMTOWDI. And I personally prefer the if/elsif/else structures - maybe I'm a bit too much of a C(++) programmer ;-)
      Cheers,
      CombatSquirrel.
      Entropy is the tendency of everything going to hell.