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>if name="Pete" then-->
<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>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
elsif age>50 then
<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>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
elsif age<20 then
<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>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
else
<span style="color: #008080;">else</span>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
end if</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if
end if<!--</lang>-->
 
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, (which avoids any dangling else problems). All conditional
expressions are short-circuited.
 
===iff===
<!--<lang Phix>var = iff(flag?true_expr:false_expr)</lang-->
<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;">)
end if<!--</lang>-->
 
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>constant DEBUG=false-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">DEBUG<span style="color: #0000FF;">=<span style="color: #004600;">false</span>
if DEBUG then
<span style="color: #008080;">if</span> <span style="color: #000000;">DEBUG</span> <span style="color: #008080;">then</span>
puts(1,"debug is on\n")
<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>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if platform()=WINDOWS then
<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>
puts(1,"this is windows\n")
<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>
elsif platform()=LINUX then
<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>
puts(1,"this is linux\n")
<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>switch v [with fallthrough] do-->
<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>
case 1,2:
<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>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
case 3 then
<span style="color: #008080;">case</span> <span style="color: #000000;">3</span> <span style="color: #008080;">then</span>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
fallthrough
<span style="color: #008080;">fallthrough</span>
case 4:
<span style="color: #008080;">case</span> <span style="color: #000000;">4<span style="color: #0000FF;">:</span>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
break
<span style="color: #008080;">break</span>
default:
<span style="color: #008080;">default<span style="color: #0000FF;">:</span>
-- do something
<span style="color: #000080;font-style:italic;">-- do something</span>
end switch</lang>
<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
7,806

edits