Help for the VSCode editor.
All the code fragments in this lab are complete mini-programs, so you can paste them into the editor and run them to see the results:
Running the code fragments
- Right click in Explorer pane to create a new file, e.g.
test.go
- Paste the question code snippet into the editor pane
- Open the terminal window and execute
go run test.go
- Re-use your
test.go
file by replacing the content with that of the next question.
Note that switch statements are nothing more than a shorthand way of writing if - else if - else if - else, where default
is the final else
-
What would be the output of the following program -
package main import "fmt" func main() { var a, b = 100, 5 switch { case a/b == 10: fmt.Println("10") case a/b == 20: fmt.Println("20") case a/b == 10: fmt.Println("30") default: fmt.Println("default") } }
- 30
- 40
- 10
- 20
Reveal
20
- Work out
a/b
.100/5 = 20
- Examine the case statements for the one that matches the above evaluation.
-
What would be the output of the following program -
package main import "fmt" func main() { day := "sunday" switch day { case "monday": fmt.Println("monday") case "tuesday": fmt.Println("tuesday") case "wednesday": fmt.Println("wednesday") case "thursday": fmt.Println("thursday") case "friday": fmt.Println("friday") case "saturday", "sunday": fmt.Println("weekend") default: fmt.Println("default") } }
- friday
sunday - weekend
default - weekend
- saturday
weekend
Reveal
weekend
- It will switch on the value of
day
which issunday
- It will match on the final case, which is checking for
saturday
orsunday
- No
default
case will be selected, as we already have a match.
- friday
-
What would be the output of the following program -
package main import "fmt" func main() { day := "wednesday" switch day { case "monday": fmt.Println("monday") case "tuesday": fmt.Println("tuesday") case "wednesday": fmt.Println("wednesday") fallthrough case "thursday": fmt.Println("thursday") fallthrough case "friday": fmt.Println("friday") case "saturday", "sunday": fmt.Println("weekend") default: fmt.Println("default") } }
- wednesday
thursday - wednesday
thursday
friday
default - wednesday
thursday
friday - wednesday
friday
default
Reveal
wednesday
thursday
friday- It will switch on the value of
day
which iswednesday
- It will match
case "wednesday"
- It then encounters
fallthrough
, which means run the code in thecase
that follows irrespective of whether the case condition matches, thusthursday
is printed. Similar forfriday
, but there's nofallthrough
in friday's case, sodefault
is not printed.
- wednesday
-
What would be the output of the following program -
package main import "fmt" func main() { var a, b = 100, 5 switch a { case a/b == 10: fmt.Println("10") case a/b == 20: fmt.Println("20") case a/b == 10: fmt.Println("30") default: fmt.Println("default") } }
- 30
- Error
- 10
- 20
Reveal
Error
At first glance this looks like Q1, but there is a subtle difference! This time it's not an unbounded switch, it is switching on the value of
a
, anda
is anint
.The comparisons for the cases are all boolean which can't be tested against the integer switch variable
a
, so you will get a compilation error. -
What would be the output of the following program -
Note: When using switch with conditionals - we don't specify expression after switch. This is what the error was in the last question.
package main import "fmt" func main() { var i, j = 10, 50 switch { case i+j == 60: fmt.Println("equal to 60") case i+j <= 60: fmt.Println("less than or equal to 60") fallthrough default: fmt.Println("greater than 60") } }
- less than or equal to 60
greater than 60 - equal to 60
- less than or equal to 60
equal to 60 - less than or equal to 60
greater than 60
Reveal
equal to 60
- Evaluate
i+j
- 10+50 = 60 - The first case matches, so
equal to 60
is printed. - There is no
fallthrough
on this case, so the switch is complete and no other case is considered.
- less than or equal to 60