4.5 KiB
What's the difference between if and switch statements?
- If statements control the execution flow but switch statements do not
- If statements are much more readable alternatives to switch statements
- Switch statements are much more readable alternatives to if statements CORRECT
1: They both control the execution flow.
2: Sometimes true, but, for complex if statements, switch statement can make them much more readable.
What type of values can you use as a switch condition?
switch condition {
// ....
}
- int
- bool
- string
- Any type of values CORRECT
4: Unlike most other C based languages, in Go, a switch statement is actually a syntactic-sugar for a if statement. This means that, Go converts a switch statement into an if statement behind the scenes. So, any type of values can be used as a condition.
What type of values can you use as the case conditions for the following switch statement?
switch false {
case condition:
// ...
}
- int
- bool CORRECT
- string
- Any type of values
2: Yes, you can only use bool values because in the example, the switch's condition is a bool.
What type of values can you use as the case conditions for the following switch statement?
switch "go" {
case condition:
// ...
}
- int
- bool
- string CORRECT
- Any type of values
3: Yes, you can only use string values because in the example, the switch's condition is a string.
What's the problem in the following code?
switch 5 {
case 5:
case 6:
case 5:
}
- Case clauses don't have any statements
- The same constants are used multiple times in case conditions CORRECT
- Switch condition cannot be an untyped int
2: That's right. 5 is used by multiple case clauses as case conditions. It should be used only once.
When the following code runs, which case will be executed?
weather := "hot"
switch weather {
case "very cold", "cold":
// ...
case "very hot", "hot":
// ...
default:
// ...
}
- None of them
- The first case clause
- The second case clause CORRECT
- The default clause
3: That's right. When switch's condition is either "hot" or "very hot", the 2nd case will be executed.
When the following code runs, which clause will be executed?
switch weather := "too hot"; weather {
default:
// ...
case "very cold", "cold":
// ...
case "very hot", "hot":
// ...
}
- None of them
- The first case clause
- The second case clause
- The default clause CORRECT
4: That's right. The switch's condition doesn't match to any of the case conditions, so the default clause will be executed as a last resort. And the default clause can be in any place inside a switch statement.
When the following code runs, which case will be executed?
switch weather := "too hot"; weather {
case "very cold", "cold":
// ...
case "very hot", "hot":
// ...
}
- None of them CORRECT
- The first case clause
- The second case clause
- The default clause
1: That's right. The switch's condition doesn't match to any of the case conditions, and there isn't a default clause. So, none of the clauses will be executed.
What does the following program print?
richter := 2.5
switch r := richter; {
case r < 2:
fallthrough
case r >= 2 && r < 3:
fallthrough
case r >= 5 && r < 6:
fmt.Println("Not important")
case r >= 6 && r < 7:
fallthrough
case r >= 7:
fmt.Println("Destructive")
}
- Nothing
- "Not important" CORRECT
- "Destructive"
2: That's right! When fallthrough is used, the following clause will be executed without checking its condition.
What's the problem in the following code?
richter := 14.5
switch r := richter; {
case r >= 5 && r < 6:
fallthrough
fmt.Println("Moderate")
default:
fmt.Println("Unknown")
case r >= 7:
fmt.Println("Destructive")
}
- default clause should be the last clause
- default clause should have a fallthrough statement
- The first case should use the fallthrough statement as its last statement CORRECT
3: That's right! In a case block, fallthrough can only be used as the last statement.
What does the following program print?
n := 8
switch {
case n > 5, n < 1:
fmt.Println("n is big")
case n == 8:
fmt.Println("n is 8")
}
- Nothing
- "n is 8"
- "n is big" CORRECT
3: That's right! Switch runs top-to-bottom and case conditions run left-to-right. Here, 1st case's 1st condition expression (which is n > 5) will yield true, so the 1st case will be executed.