Complete the code to print the correct message based on the value of x.
package main import "fmt" func main() { x := 2 switch x { case [1]: fmt.Println("Two") default: fmt.Println("Other") } }
The switch checks the value of x. To print "Two", the case must match 2.
Complete the switch to print "Even" when n is 2 or 4.
package main import "fmt" func main() { n := 4 switch n { case 2, [1]: fmt.Println("Even") default: fmt.Println("Odd") } }
The switch case lists values that match n. To print "Even" for 2 and 4, include 4 in the case.
Fix the error in the switch expression to correctly print "Hello" when s is "hi".
package main import "fmt" func main() { s := "hi" switch [1] { case "hi": fmt.Println("Hello") default: fmt.Println("Bye") } }
The switch expression should be the variable s to compare its value to cases.
Fill both blanks to create a switch that prints "Small" for values less than 10 and "Large" otherwise.
package main import "fmt" func main() { num := 7 switch { case num [1] 10: fmt.Println("Small") default: fmt.Println("Large") } }
In a switch without an expression, each case is a condition. To print "Small" when num is less than 10, use <.
Fill all three blanks to create a switch that prints the day type based on the day number (1-7).
package main import "fmt" func main() { day := 6 switch day { case [1], [2], [3]: fmt.Println("Weekend") default: fmt.Println("Weekday") } }
Days 6 and 7 are weekend days in this example. The switch case lists these days to print "Weekend".