Conditional structures: Difference between revisions

Content added Content deleted
(Add example for Grain)
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
Line 8,323: Line 8,323:
<syntaxhighlight lang="vbnet">Dim result As String = If("pants" = "glasses", "passed", "failed") ' type is inferred</syntaxhighlight>
<syntaxhighlight lang="vbnet">Dim result As String = If("pants" = "glasses", "passed", "failed") ' type is inferred</syntaxhighlight>


=={{header|Vlang}}==
=={{header|V (Vlang)}}==
If and match are the general purpose conditional structures in Vlang, although the language certainly contains other conditional elements.
If and match are the general purpose conditional structures in V (Vlang), although the language certainly contains other conditional elements.
===If===
===If===
Simplest usage is,
Simplest usage is,
<syntaxhighlight lang="vlang">if boolean_expression {
<syntaxhighlight lang="v (vlang)">if boolean_expression {
statements
statements
}</syntaxhighlight>
}</syntaxhighlight>
The braces are required, even around a single statement.
The braces are required, even around a single statement.
<syntaxhighlight lang="vlang">if boolean_expression {
<syntaxhighlight lang="v (vlang)">if boolean_expression {
statements
statements
} else {
} else {
Line 8,338: Line 8,338:
}</syntaxhighlight>
}</syntaxhighlight>
Braces are required around else clauses, as above, unless the statement of the else clause is another if statement. In this case the statements are chained like this,
Braces are required around else clauses, as above, unless the statement of the else clause is another if statement. In this case the statements are chained like this,
<syntaxhighlight lang="vlang">if boolean_expression1 {
<syntaxhighlight lang="v (vlang)">if boolean_expression1 {
statements
statements
} else if boolean_expression2 {
} else if boolean_expression2 {
Line 8,347: Line 8,347:
===Match===
===Match===
Simple usage is,
Simple usage is,
<syntaxhighlight lang="vlang">match true {
<syntaxhighlight lang="v (vlang)">match true {
boolean_expression1 {
boolean_expression1 {
statements
statements
Line 8,364: Line 8,364:


Match can also switch on the value of an expression, as in,
Match can also switch on the value of an expression, as in,
<syntaxhighlight lang="vlang">switch expression_of_any_type {
<syntaxhighlight lang="v (vlang)">switch expression_of_any_type {
value1 {
value1 {
statements
statements