Conditional structures: Difference between revisions

m
→‎case without a default: the error discussed isn’t syntactic
(→‎Else and elif: there really is no need to use semicolons if we plan on writing several lines anyway)
m (→‎case without a default: the error discussed isn’t syntactic)
 
(5 intermediate revisions by 5 users not shown)
Line 2:
{{Control Structures}}
[[Category:Simple]]
[[Category:Flow control]]
 
;Task:
Line 587 ⟶ 588:
);</syntaxhighlight>
====case expressions====
Using the same example above, we assume that the <syntaxhighlight lang="ada">''Operation</syntaxhighlight>'', <syntaxhighlight lang="ada">''Op</syntaxhighlight>'', and <syntaxhighlight lang="ada">''Result</syntaxhighlight>'' variables are declared. A case expression over the enumeration of operations might look like:
<syntaxhighlight lang="ada">Result := (case Op is
Add => A + B,
Line 596 ⟶ 597:
</syntaxhighlight>
Note: some websites (particularly [https://www.radford.edu/nokie/classes/320/abe/operators.html#:~:text=if%20and%20case-,Examples%3A,1%2C%20others%20%3D%3E%200)%3B this one]) contain a different variant of a case expression (<syntaxhighlight lang="ada">case Op of...</syntaxhighlight>). The Ada Reference Manual indicates this is incorrect, and we use the [http://www.ada-auth.org/standards/12rm/html/RM-4-5-7.html formal version] here.
 
===case with a default alternative===
<syntaxhighlight lang="ada">type Days is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
Line 613 ⟶ 615:
===case without a default===
When there is no '''when others''' clause, the compiler will complain about any uncovered alternative. This defends against a common reason for bugs in other languages.
I.e., the following code is syntactically incorrect:
 
<syntaxhighlight lang="ada">case Today is
Line 625 ⟶ 627:
end case;</syntaxhighlight>
 
The syntactically correct version:
 
<syntaxhighlight lang="ada">case Today is
Line 1,743 ⟶ 1,745:
=={{header|Binary Lambda Calculus}}==
 
Lambda calculus has no conditional structures built in, but the standard representations of booleans can be seen to implement if-then-else: true = \then. \else. then, false = \then. \else. else, which correspond to BLC programs <code>00 00 110</code> and <code>00 00 10</code>.
 
=={{header|blz}}==
Line 3,893 ⟶ 3,895:
 
=={{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 ⟶ 3,919:
 
===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 ⟶ 3,945:
}
 
givenswitch[and] .x, .y, .z {
case true: ...
# all are true
Line 3,952 ⟶ 3,954:
}</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 ⟶ 3,967:
 
===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,979:
}</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>
Line 6,909 ⟶ 6,912:
}}
</syntaxhighlight>
 
=={{header|RPL}}==
'''IF..THEN'''
'''IF''' <instruction(s)> '''THEN''' <instruction(s)> '''END'''
'''IF..THEN..ELSE'''
'''IF''' <instruction(s)> '''THEN''' <instruction(s)> '''ELSE''' <instruction(s)> '''END'''
Instructions between <code>IF</code> and <code>THEN</code> are not mandatory, but recommended for lisibility. The interpreter considers <code>IF</code> as a null word and performs branching when meeting the word <code>THEN</code>: if stack level 1 is not equal to zero, the instructions between <code>THEN</code> and <code>END</code> will be executed.
 
<code>IFT</code> and <code>IFTE</code> are stack-based conditonal structures. <code>IFT</code> evaluates the content of stack level 1 only if the content of stack level 2 is not zero, otherwise it is dropped. <code>IFTE</code> evaluates the content of stack level 1 if the content of stack level 2 is zero, otherwise if evaluates the content of stack level 2.
 
'''CASE..END'''
'''CASE'''
<instruction(s)> '''THEN''' <instruction(s)> '''END'''
<instruction(s)> '''THEN''' <instruction(s)> '''END'''
<span style="color:grey">@ as many branches as needed</span>
<instruction(s)> <span style="color:grey">@ default branch (optional)</span>
'''END'''
 
=={{header|Ruby}}==
6

edits