3-1 條件?迴???

` Perl cAjiUCƤjG bԭzA̱`NO if ԭzA@榡pUG If (condition1) { K } elsif (condition2) { K } else { ... } 䤤uelsifviHƫܦhCҦp]tif.pl^G

lɡ]tif.pl^G]ǦϰUYi^
@alldays = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
$num = rand(scalar @alldays);
$thisday = @alldays[$num];
if ($thisday eq "Sat") {
	$type = "holiday";
} elsif ($thisday eq "Sun") {
	$type = "holiday";
} else {
	$type = "workday";
}
print("$thisday is a $type.");

bWdҤAuscalar @alldaysv|Ǧ^ 7AN @alldays ӼơAurand(scalar @alldays)vh|Ǧ^@Ӥ [0, 7) ϶üơA] $thisday ȴNiO $alldays @ӤCY $thisday O "Sat" "Sun"Ah $type O "holiday"A_hA$type$ O "workday"C

²檺 if ԭzAڭ̤]iH if 󳡤᭱AҦpAUCC{XOĪG

if ($x > 0) {print("$x is positive");} print("$x is positive") if ($x > 0); WzĤGC{XOŦXfykAb@q{yoܤ֨C

t@ӱԭzO unlessAObP_󤣬upUA~᪺ԭzCҦpG

unless ($x <= 0) { print("$x is positive"); } M if ԭzۦPAڭ̤]iH unless P_h᭱C

XWAڭ̥ioıAUC{X\O@˪G

1. if ($x > 0) {print "positive\n";}; 2. print "positive\n" if ($x > 0); 3. unless ($x <= 0) {print "positive\n";}; 4. print "positive\n" unless ($x <= 0); 5. $x > 0 && print "positive\n"; 6. $x <= 0 || print "positive\n"; bWҲĤCAu&&vN޿誺uANDvBA]unĤ@ӹBlO 0]Y $x > 0^AhAĤGӹBl]YLX "positive"^CPzAbĤCAu||vN޿誺uORvBA]unĤ@ӹBlO 0]Y $x <= 0^AhACLʧ@C

bԭzAPerl pP_@ӹB⦡O True ]u^ False ]^OHھڪhpUG

  1. NB⵲G@ӦrC
  2. pGB⵲GO "0" ""]Ŧr^ undef ]wq^AhNG FalseA_hY TrueC
HUoǽdŪ̦ۦѦҤαQG

ԭzPwG
0False
1-1False
1True
""False
"1"True
"00"True
"0.000"True
undefFalse

Uӧڭ̭nOjԭz]Loop Statement^CO while/until jԭzA򥻮榡pUG

while () { j餺ԭz } until () { j餺ԭz } do { j餺ԭz } while (); do { j餺ԭz } until (); eخ榡OP_󦡡AAMwO_j餺ԭzFخ榡Oj餺ԭzAAP_󦡡AMwO_~jC

t@ӱ`Ψ쪺jԭzO for jA榡M C y for jCbUҤAڭ̧Q for jN}Cv@LXG

@days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); for ($i=0; $i<7; $i++) { print("$days[$i]"; } Perl ]ѤFt@رMAΩ}Cܼƪ foreach jAڭ̥i foreach jNWz{XgpUG @days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); foreach $element (@days) { print("$element"); } 䤤 $element ȷ|̧dzQ]w}C @days C@ӤAMA᪺CLʧ@CƹWAWҥiHAQ²ƦpUG @days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); foreach (@days) { print; } yܻA$elementwQٲAPerl |ۦΤ@ӵܼ $_ ӨN $element AܼƤ]O foreach M print w]޼ơ]Default Argument^C

Ynqj餤YXAi lastAĪGM C y break OۦPCҦpAbBz}C $array ɡAY@ӤȬtƮɡAhXjAåѰj餧᪺ԭz}lA{XG

for ($i = 0; $i < 10; $i++) { last if $array[$i] < 0; K } bt@رpUAڭ̥iݭnj骺AåߨܤU@Ӱj骺}lmӰA\i next ӹFAĪGM C y continue OۦPCҦpAbBz}C $array ɡAY@ӤȬtƮɡAhߨXjAåѤUj}lA{XG foreach $element (@array) { next if $element < 0; ... } Ѧڭ̥iݥXAPerl last ۷ C breakA Perl next ۷ C continueC
Perl