Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
To me, the following two codes are identical and are both as simple to make. It does the EXACT same thing and the syntax isn't so different, so why would we use switch instead of elsif?
switch ($wday) { case 0 { print "today is Sun"; } case 1 { print "today is Mon"; } case 2 { print "today is Tue"; } case 3 { print "today is Wed"; } case 4 { print "today is Thu"; } case 5 { print "today is Fri"; } case 6 { print "today is Sat"; } } if ($wday eq "0") { print "today is Sun"; } elsif($wday eq "1") { print "today is Mon"; } elsif($wday eq "2") { print "today is Tue"; } elsif($wday eq "3") { print "today is Wed"; } elsif($wday eq "4") { print "today is Thu"; } elsif($wday eq "5") { print "today is Fri"; } elsif($wday eq "6") { print "today is Sat"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using Switch;
by Forsaken (Friar) on May 12, 2005 at 06:22 UTC | |
|
Re: using Switch;
by rnahi (Curate) on May 12, 2005 at 05:56 UTC | |
|
Re: using Switch;
by tlm (Prior) on May 12, 2005 at 05:58 UTC | |
|
Re: using Switch;
by CountZero (Bishop) on May 12, 2005 at 05:42 UTC |