Conditional structures: Difference between revisions

→‎{{header|Perl 6}}: add missing stuff
(→‎{{header|friendly interactive shell}}: Creation of fish example)
(→‎{{header|Perl 6}}: add missing stuff)
Line 2,531:
 
=={{header|Perl 6}}==
===if/else===
 
<codett>if</codett>, <codett>else</codett>, <codett>elsif</codett>, <codett>unless</codett>, and <codett>given</codett> work much as they do in Perl 5, with the following differences:<ul>
<li> All the parentheses are now optional.
</li><li> <codett>unless</codett> no longer permits <codett>elsif</codett> or <codett>else</codett> blocks.
</li><li> If the block of an <codett>if</codett>, <codett>elsif</codett>, or <codett>unless</codett> has a nonzero arity, the value of the conditional expression is used as an argument to the block:
<lang perl6>if won() -> $prize {
say "You won $prize.";
}</lang>
If an <codett>else</codett> block has a nonzero arity, it recievesreceives the value of the condition tested by the last <codett>if</codett> or <codett>elsif</codett>. </li></ul>
===given/when===
 
Switch structures are done by topicalization and by smartmatching in Perl 6. They are somewhat orthogonal, you can use a <tt>given</tt> block without <tt>when</tt>, and vice versa. But the typical use is:
<code>when</code> blocks are now allowed "in any block that sets <code>$_</code>, including a
<lang perl6>given lc prompt("Done? ") {
<code>for</code> loop (assuming one of its loop variables is bound to <code>$_</code>)
when 'yes' { return }
or the body of a method (if you have declared the invocant as <code>$_</code>)." See [http://perlcabal.org/syn/S04.html#Switch_statements Synopsis 4].
when 'no' { next }
 
default { say "Please answer either yes or not." }
The ternary operator now looks like this:
}</lang>
<codett>when</codett> blocks are now allowed "in any block that setstopicalizes <codett>$_</codett>, including a
<codett>for</codett> loop (assuming one of its loop variables is bound to <codett>$_</codett>)
or the body of a method (if you have declared the invocant as <codett>$_</codett>)." See [http://perlcabal.org/syn/S04.html#Switch_statements Synopsis 4].
 
There are also statement modifier forms of all of the above.
===Ternary operator===
The ternary operator now looks like this:
<lang perl6>$expression ?? do_something !! do_fallback</lang>
===Other short-circuiting operators===
 
<codett>and</codett>, <codett>or</codett>, <codett>&&</codett>, and <codett>||</codett> and <tt>//</tt> work as in Perl 5.
 
=={{header|PHP}}==
Anonymous user