Skip to content

Latest commit

 

History

History
239 lines (180 loc) · 6.09 KB

07-switch-statement.md

File metadata and controls

239 lines (180 loc) · 6.09 KB

Lab: Switch Statement

Take me to the lab!

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
  1. Right click in Explorer pane to create a new file, e.g. test.go
  2. Paste the question code snippet into the editor pane
  3. Open the terminal window and execute go run test.go
  4. 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

Questions

  1. 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

    1. Work out a/b. 100/5 = 20
    2. Examine the case statements for the one that matches the above evaluation.
  2. 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 is sunday
    • It will match on the final case, which is checking for saturday or sunday
    • No default case will be selected, as we already have a match.
  3. 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 is wednesday
    • It will match case "wednesday"
    • It then encounters fallthrough, which means run the code in the case that follows irrespective of whether the case condition matches, thus thursday is printed. Similar for friday, but there's no fallthrough in friday's case, so default is not printed.
  4. 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, and a is an int.

    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.

  5. 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

    1. Evaluate i+j - 10+50 = 60
    2. The first case matches, so equal to 60 is printed.
    3. There is no fallthrough on this case, so the switch is complete and no other case is considered.