switch


Node.js

  1. const value = 'b'
  2. switch(value) {
  3. case 'a':
  4. console.log('A')
  5. break
  6. case 'b':
  7. console.log('B')
  8. break
  9. case 'c':
  10. console.log('C')
  11. break
  12. default:
  13. console.log('first default')
  14. }
  15. switch(value) {
  16. case 'a':
  17. console.log('A - falling through')
  18. case 'b':
  19. console.log('B - falling through')
  20. case 'c':
  21. console.log('C - falling through')
  22. default:
  23. console.log('second default')
  24. }

Output

  1. B
  2. B - falling through
  3. C - falling through
  4. second default

Go

  1. package main
  2. import "fmt"
  3. func main() {
  4. value := "b"
  5. switch value {
  6. case "a":
  7. fmt.Println("A")
  8. case "b":
  9. fmt.Println("B")
  10. case "c":
  11. fmt.Println("C")
  12. default:
  13. fmt.Println("first default")
  14. }
  15. switch value {
  16. case "a":
  17. fmt.Println("A - falling through")
  18. fallthrough
  19. case "b":
  20. fmt.Println("B - falling through")
  21. fallthrough
  22. case "c":
  23. fmt.Println("C - falling through")
  24. fallthrough
  25. default:
  26. fmt.Println("second default")
  27. }
  28. }

Output

  1. B
  2. B - falling through
  3. C - falling through
  4. second default