# Conditional - Switch Statement



In this section, we are going to look at the `switch` statement. This is all part of [control flow](https://en.wikipedia.org/wiki/Control_flow), which is sequential, iterative (loops), and conditional; control flow is how computers read your program.

By default, it is sequential. You can control the _flow_ of how computers are reading your program with something like an iterative, using the `for` keyword, you can also use a conditional; an `if` statement or a `switch` statement.

A `switch` statement always starts with the keyword `switch`, think "Hey, I want to switch on something."

In addition to the `switch` keyword, a `switch` statement has `case`s. The `switch` statement switches on some `case`.

```go
package main

import (
	"fmt"
)

func main() {
	switch {
	case false:
		fmt.Println("this should not print")
	case (2 == 4):
		fmt.Println("this should not print2")
	case (3 == 3):
		fmt.Println("prints")
	case (4 == 4):
		fmt.Println("also true, does it print?")
	}
}
```

[playground](https://play.golang.org/p/Vk9yIJGkKy)

In the above, the first `case` that evaluates to true `3==3` runs the code speficied `fmt.Println("prints")` and the switch statement exits. There is no fallthrough by default in a `switch` statement in Go, unless it is specified, which is why the final`case` here `4==4` does not return. Let's specify `fallthrough` and see what happens.

```go
package main

import (
	"fmt"
)

func main() {
	switch {
	case false:
		fmt.Println("this should not print")
	case (2 == 4):
		fmt.Println("this should not print2")
	case (3 == 3):
		fmt.Println("prints")
		fallthrough
	case (4 == 4):
		fmt.Println("also true, does it print?")
	}
}
```

[playground](https://play.golang.org/p/1EtC2k2rvX)

Because we specified `fallthrough`, the final case `4==4` also gets run.  You can use `fallthrough` to make each statement evaluate, as in this example.

```go
package main

import (
	"fmt"
)

func main() {
	switch {
	case false:
		fmt.Println("this should not print")
	case (2 == 4):
		fmt.Println("this should not print2")
	case (3 == 3):
		fmt.Println("prints")
		fallthrough
	case (4 == 4):
		fmt.Println("also true, does it print?")
		fallthrough
	case (7 == 9):
		fmt.Println("not true 1")
		fallthrough
	case (11 == 14):
		fmt.Println("not true 2")
		fallthrough
	case (15 == 15):
		fmt.Println("true 15")
	}
}
```

[playground](https://play.golang.org/p/QK2Tu6x-SL)

However, generally speaking just don't use `fallthrough`.

We can also add a `default` case, which is what happens if nothing else evaluates to true.

```go
package main

import (
	"fmt"
)

func main() {
	switch {
	case false:
		fmt.Println("this should not print")
	case (2 == 4):
		fmt.Println("this should not print2")
	default:
	 	fmt.Println("this is default")
	}
}
```

[playground](https://play.golang.org/p/pINfFTpk30)

If you have a value after the `switch` statement, it no longer evaluates on a boolean, but rather evaluates on the value.

```go
package main

import (
	"fmt"
)

func main() {
	switch "Bond" {
	case "Moneypenny":
		fmt.Println("miss money")
	case "Bond":
		fmt.Println("bond james")
	case "Q":
		fmt.Println("This is q")
	default:
	 	fmt.Println("this is default")
	}
}
```

[playgound](https://play.golang.org/p/ixwqQk_XPA)

Rather than hard code the `switch` statement value, we could use a variable.

```go
package main

import (
	"fmt"
)

func main() {
	n := "Bond"
	switch n {
	case "Moneypenny":
		fmt.Println("miss money")
	case "Bond":
		fmt.Println("bond james")
	case "Q":
		fmt.Println("This is q")
	default:
	 	fmt.Println("this is default")
	}
}
```

[playground](https://play.golang.org/p/jXeN5Bqn3Y)

We can also have multiple values for a `case` statement, as in `case "Moneypenny", "Bond", "Dr No"`

```go
package main

import (
	"fmt"
)

func main() {
	n := "Bond"
	switch n {
	case "Moneypenny", "Bond", "Dr No":
		fmt.Println("miss money or bond or dr no")
	case "M":
		fmt.Println("m")
	case "Q":
		fmt.Println("This is q")
	default:
	 	fmt.Println("this is default")
	}
}
```

[playground](https://play.golang.org/p/WNz5dijV4-)

