Conditional structures: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
|||
Line 3,893: | Line 3,893: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
If and switch |
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=== |
||
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 |
===switch expressions=== |
||
Switch |
Switch expressions are highly flexible, and it is not all covered here. See langurlang.org for details. |
||
Switch |
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: | ||
} |
} |
||
switch[and] .x, .y, .z { |
|||
case true: ... |
case true: ... |
||
# all are true |
# all are true |
||
Line 3,952: | Line 3,952: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Complex test expressions are evaluated once, then compared against conditions. |
|||
===implicit fallthrough=== |
===implicit fallthrough=== |
||
If a block of a switch |
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"> |
<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 |
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"> |
<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 |
===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"> |
<syntaxhighlight lang="langur">switch(.x, .y, .z; |
||
true: ... ; # |
true: ... ; # any are equal to true |
||
_, >= .z: ...; # .y >= .z |
_, >= .z: ...; # .y >= .z |
||
... ) # default</syntaxhighlight> |
... ) # default</syntaxhighlight> |