The following exercises let you explore switch statements and goto.
-
Rewrite exercise 1 using a switch statement. Which version do you find more readable? Why?
-
The following code should break out of the for loop when i == 5, but it doesn’t. Why not? Identify the cause and fix it:
go
for i:= range 10 {
switch i {
case 5: break
default: fmt.Println(i)
}
}
- Rewrite the code in the
exercises/ex9directory to use goto. Is it easier or harder to follow now? Why?
Progress is saved on this device.