Conditional structures: Difference between revisions

Line 3,893:
 
=={{header|langur}}==
If and switch/given expressions always produce a value, even if it's nothing (null).
 
In the shortened forms, you dispense with the keywords (except the first one).
Line 3,917:
 
===simple if===
Langur 0.7.1 added simpleSimple if expressions (previously erroneously called "if statements") usinguse a colon after the test condition. This is convenient for simple branching or assignments, without having to use curly braces, but does not allow for else if or else sections.
 
<syntaxhighlight lang="langur">if .x > .y: break</syntaxhighlight>
 
===switch/given expressions===
Switch and given expressions are highly flexible, and it is not all covered here. See langurlang.org for details.
 
Switch was added to langur 0.11. The difference between switch and given is that switch defaults to testing that any condition is true (as familiar to many programmers), andbut giventhis defaultscan tobe testingchanged thatby allspecifying conditionsan areinfix trueoperator within square brackets, such as switch[and].
 
<syntaxhighlight lang="langur">switch .x, .y, .z {
Line 3,943:
}
 
givenswitch[and] .x, .y, .z {
case true: ...
# all are true
Line 3,952:
}</syntaxhighlight>
 
As of 0.7, complexComplex test expressions are evaluated once, then compared against conditions.
 
===implicit fallthrough===
If a block of a switch or given has any statements, there is no implicit fallthrough. A case with an empty block after it creates an implicit fallthrough.
<syntaxhighlight lang="langur">givenswitch .x {
case true:
# implicit fallthrough
Line 3,965:
 
===explicit fallthrough from anywhere===
A fallthrough statement is allowed anywhere within a switch or given block, not just at the end. (It's typical in programming languages to only allow fallthrough at the end of the block.)
 
<syntaxhighlight lang="langur">givenswitch .x {
case true:
if .y > 100 {
Line 3,976 ⟶ 3,977:
}</syntaxhighlight>
 
===shortened form switch/given===
A shortened form expects a single action expression per test and is more limited in other ways, as well (no explicit fallthrough, no alternate test expressions, no alternate logical operators). A default section is optional (null by default).
<syntaxhighlight lang="langur">givenswitch(.x, .y, .z;
true: ... ; # allany are equal to true
_, >= .z: ...; # .y >= .z
... ) # default</syntaxhighlight>
890

edits