Conditional structures: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Go}}: language change. braces required on else clauses.) |
||
Line 1,101: | Line 1,101: | ||
statements |
statements |
||
}</lang> |
}</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 go>if booleanExpression { |
|||
statements |
|||
} else singleStatement</lang> |
|||
Since an if statement ''is'' a statment, chains of if/else if statements can be written like this: |
|||
<lang go>if booleanExpression1 { |
<lang go>if booleanExpression1 { |
||
statements |
statements |
||
Line 1,112: | Line 1,108: | ||
} |
} |
||
</lang> |
</lang> |
||
Neither of the last two examples are "best practice" however. In the first case, it is common to put braces around the single statement anyway. In the second case, a select statement is preferred. |
|||
If allows a statement to be included ahead of the condition. This is commonly a short variable declaration, as in, |
If allows a statement to be included ahead of the condition. This is commonly a short variable declaration, as in, |
||
<lang go>if x := fetchSomething(); if x > 0 { |
<lang go>if x := fetchSomething(); if x > 0 { |