Conditional structures: Difference between revisions

→‎{{header|Go}}: language change. braces required on else clauses.
No edit summary
(→‎{{header|Go}}: language change. braces required on else clauses.)
Line 1,101:
statements
}</lang>
TheBraces aboveare showsrequired thearound idiomaticelse wayclauses, toas writeabove, anunless if/else.the statement Ifof the else clause containsis onlyanother a singleif statement. however, itIn isthis legal to leave offcase the bracesstatements aroundare thatchained single statement, aslike inthis,
<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 {
statements
Line 1,112 ⟶ 1,108:
}
</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,
<lang go>if x := fetchSomething(); if x > 0 {
1,707

edits