Jump to content

Conditional structures: Difference between revisions

Go explanation
(Go explanation)
Line 926:
anew(1:n,1:m) = a(1:n,1:m)
end where</lang>
=={{header|Go}}==
If and switch are the general purpose conditional structures in Go, although the language certainly contains other conditional elements.
===If===
Simplest usage is,
<lang go>if booleanExpression {
statements
}</lang>
The braces are required, even around a single statement.
<lang go>if booleanExpression {
statements
} else {
other
statements
}</lang>
The above shows the idiomatic way to write an if/else. If the else clause contains only a single statement however, it is legal to leave off the braces around that single statement, as in,
<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
} else if booleanExpression2 {
otherStatements
}
</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 {
DoPos(x)
} else {
DoNeg(x)
}</lang>
In this case the scope of x is limited to if statement.
===Switch===
Simple usage is,
<lang go>switch {
case booleanExpression1:
statements
case booleanExpression2:
other
statements
default:
last
resort
statements
}</lang>
Because switch can work with any number of arbitrary boolean expressions, it replaces if/elseif chains often found in other programming languages.
 
Switch can also switch on the value of an expression, as in,
<lang go>switch expressionOfAnyType {
case value1:
statements
case value2, value3, value4:
other
statements
}</lang>
As shown, multiple values can be listed for a single case clause.
Since go is statically typed, the types of value1, 2, 3, and 4 must match the type of the expression.
 
As with if, a local statement such as a short variable declaration can precede the expression. If there is no expression, the statement is still marked by a semicolon:
<lang go>switch x := fetch(); {
case x == "cheese":
statements
case otherBooleanExpression:
other
statements
}</lang>
Also, as with if, the scope of x is limited to the switch statement.
 
Execution does not normally fall through from one case clause to the next, but this behavior can be forced with a fallthrough statement.
 
An interesting example:
<lang go>switch {
case booleanExpression1:
default:
statements
preliminaryToOtherStatements
fallthrough
case booleanExpression2:
other
statements
}</lang>
Case expressions are evaluated in order, then if none are true, the default clause is executed.
 
Another statement that interacts with switch is break. It breaks from the switch statement and so will not break from a surrounding for statement. The following example prints "I want out!" endlessly.
<lang go>for {
switch {
case true:
break
}
fmt.Println("I want out!")
}</lang>
Labels provide the desired capability. The following prints "I'm off!"
<lang go>treadmill: for {
switch {
case true:
break treadmill
}
}
fmt.Println("I'm off!")</lang>
 
=={{header|Haskell}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.