Anonymous user
Conditional structures: Difference between revisions
→{{header|Perl}}: improve style, language, formatting
m (added a ;Task: (bold) header, added other whitespace to the task's preamble.) |
(→{{header|Perl}}: improve style, language, formatting) |
||
Line 3,303:
=={{header|Perl}}==
{{works with|Perl|5}}
===if/else===
if ($expression) {▼
do_something;▼
# postfix conditional▼
do_something if $expression;▼
}</lang>
if ($expression) {▼
do_something;▼
} else {▼
do_fallback;▼
}
}</lang>
}
}
}</lang>
▲ if ($expression1) {
▲ do_something;
▲ } elsif ($expression2) {
▲ do_something_different;
▲ } else {
▲ do_fallback;
===unless===
''unless'' behaves like ''if'', only logically negated. You can use it wherever you can use ''if''. An ''unless'' block can have ''elsif'' and ''else'' blocks, but there is no ''elsunless''.▼
<code>unless</code> behaves like <code>if</code>, only logically negated.
▲
===ternary operator===
The ternary operator is used as an expression within a statement, rather than as a control flow structure containing one or more statements. It is frequently used in assignment, or sometimes for passing function call arguments that vary depending on some condition.
===logical operators===
<code>$condition and do_something</code> is equivalent to <code>$condition ? do_something : $condition</code>.▼
<
▲<
<code>&&</code> and <code>||</code> have the same semantics as <code>and</code> and <code>or</code>, respectively, but their precedence is much higher, making them better for conditional expressions than control flow.
===switch===
At
{{works with|Perl|5.10}}
|