Conditional structures: Difference between revisions

no edit summary
(add Zig example)
No edit summary
Line 7,733:
 
<lang vbnet>Dim result As String = If("pants" = "glasses", "passed", "failed") ' type is inferred</lang>
 
=={{header|Vlang}}==
If and match are the general purpose conditional structures in Vlang, although the language certainly contains other conditional elements.
===If===
Simplest usage is,
<lang vlang>if boolean_expression {
statements
}</lang>
The braces are required, even around a single statement.
<lang vlang>if boolean_expression {
statements
} else {
other
statements
}</lang>
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,
<lang vlang>if boolean_expression1 {
statements
} else if boolean_expression2 {
otherStatements
}
</lang>
 
===Match===
Simple usage is,
<lang vlang>match true {
boolean_expression1 {
statements
}
boolean_expression2 {
other
statements
}
else {
last
resort
statements
}
}</lang>
Because match can work with any number of arbitrary boolean expressions, it replaces if/elseif chains often found in other programming languages.
 
Match can also switch on the value of an expression, as in,
<lang vlang>switch expression_of_any_type {
value1 {
statements
}
value2, value3, value4 {
other
statements
}
else {}
}</lang>
As shown, multiple values can be listed for a single case clause.
Since vlang is statically typed, the types of value1, 2, 3, and 4 must match the type of the expression.
 
=={{header|Vorpal}}==
338

edits