Conditional structures: Difference between revisions
m
→{{header|Phix}}: added syntax colouring the hard way, phix/basics
m (→{{header|Ada}}: This edit adds conditional expressions added in Ada 2012.) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 4,901:
=={{header|Phix}}==
{{libheader|Phix/basics}}
===if===
<!--<lang Phix>
<span style="color: #008080;">if</span> <span style="color: #000000;">name<span style="color: #0000FF;">=<span style="color: #008000;">"Pete"</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">age<span style="color: #0000FF;">><span style="color: #000000;">50</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">age<span style="color: #0000FF;"><<span style="color: #000000;">20</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
<span style="color: #008080;">else</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
end if</lang>▼
<span style="color: #008080;">end</span> <span style="color: #008080;">if
There is no limit to the number of elsif clauses, including 0. The else clause is also optional,
whereas the end if is always mandatory,
expressions are short-circuited.
===iff===
<!--<lang Phix>
<span style="color: #000000;">somevar</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff<span style="color: #0000FF;">(<span style="color: #000000;">flag<span style="color: #0000FF;">?<span style="color: #000000;">true_expr<span style="color: #0000FF;">:<span style="color: #000000;">false_expr<span style="color: #0000FF;">)
In an iff() expression, only one of true_expr or false_expr will be evaluated, not both.
Line 4,925 ⟶ 4,930:
first if statement, and in the second the conditions are evaluated at compile-time and code is only
emitted for one of the branches.
<!--<lang Phix>
<span style="color: #008080;">constant</span> <span style="color: #000000;">DEBUG<span style="color: #0000FF;">=<span style="color: #004600;">false</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">DEBUG</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"debug is on\n"<span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">=<span style="color: #000000;">WINDOWS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"this is windows\n"<span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">platform<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">=<span style="color: #000000;">LINUX</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"this is linux\n"<span style="color: #0000FF;">)</span>
▲end if</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if
<!--</lang>-->
===switch===
<!--<lang Phix>
<span style="color: #008080;">switch</span> <span style="color: #000000;">v</span> <span style="color: #000080;font-style:italic;">/*with fallthrough*/</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">2<span style="color: #0000FF;">:</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">3</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
<span style="color: #008080;">fallthrough</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">4<span style="color: #0000FF;">:</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
<span style="color: #008080;">break</span>
<span style="color: #008080;">default<span style="color: #0000FF;">:</span>
<span style="color: #000080;font-style:italic;">-- do something</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch
<!--</lang>-->
By default there is no fallthough on switch clauses, however you can add(/uncomment) a directive, and you can
override individual clauses with explicit fallthough or break statements. There is no need to have
break between cases when it is the default. You can use either : or then on case clauses. The else
|