Conditional structures: Difference between revisions

Content added Content deleted
Line 3,893: Line 3,893:


=={{header|langur}}==
=={{header|langur}}==
If and switch/given expressions always produce a value, even if it's nothing (null).
If and switch expressions always produce a value, even if it's nothing (null).


In the shortened forms, you dispense with the keywords (except the first one).
In the shortened forms, you dispense with the keywords (except the first one).
Line 3,917: Line 3,917:


===simple if===
===simple if===
Langur 0.7.1 added simple if expressions (previously erroneously called "if statements") using 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.
Simple if expressions use 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>
<syntaxhighlight lang="langur">if .x > .y: break</syntaxhighlight>


===switch/given expressions===
===switch expressions===
Switch and given expressions are highly flexible, and it is not all covered here. See langurlang.org for details.
Switch 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) and given defaults to testing that all conditions are true.
Switch defaults to testing that any condition is true (as familiar to many programmers), but this can be changed by specifying an infix operator within square brackets, such as switch[and].


<syntaxhighlight lang="langur">switch .x, .y, .z {
<syntaxhighlight lang="langur">switch .x, .y, .z {
Line 3,943: Line 3,943:
}
}


given .x, .y, .z {
switch[and] .x, .y, .z {
case true: ...
case true: ...
# all are true
# all are true
Line 3,952: Line 3,952:
}</syntaxhighlight>
}</syntaxhighlight>


As of 0.7, complex test expressions are evaluated once, then compared against conditions.
Complex test expressions are evaluated once, then compared against conditions.


===implicit fallthrough===
===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.
If a block of a switch has any statements, there is no implicit fallthrough. A case with an empty block after it creates an implicit fallthrough.
<syntaxhighlight lang="langur">given .x {
<syntaxhighlight lang="langur">switch .x {
case true:
case true:
# implicit fallthrough
# implicit fallthrough
Line 3,965: Line 3,965:


===explicit fallthrough from anywhere===
===explicit fallthrough from anywhere===
A fallthrough statement is allowed anywhere within a switch or given block, not just at the end.
A fallthrough statement is allowed anywhere within a switch 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">given .x {
<syntaxhighlight lang="langur">switch .x {
case true:
case true:
if .y > 100 {
if .y > 100 {
Line 3,976: Line 3,977:
}</syntaxhighlight>
}</syntaxhighlight>


===shortened form switch/given===
===shortened form switch===
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).
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">given(.x, .y, .z;
<syntaxhighlight lang="langur">switch(.x, .y, .z;
true: ... ; # all are equal to true
true: ... ; # any are equal to true
_, >= .z: ...; # .y >= .z
_, >= .z: ...; # .y >= .z
... ) # default</syntaxhighlight>
... ) # default</syntaxhighlight>