Golang Switch | Find Output Programs | Set 2

This section contains the Golang switch find output programs (set 2) with their output and explanations.
Submitted by Nidhi, on August 11, 2021

Program 1:

package main  

import "fmt"  

func main() {
  switch 30 {  
    case 11 to 20:  
      fmt.Println("Between 11 to 20")
    case 21 to 30:  
      fmt.Println("Between 21 to 30")
    case 31 to 40:  
      fmt.Println("Between 31 to 40")
    case 41 to 50:  
      fmt.Println("Between 41 to 50")
    default:  
      fmt.Println("INVALID VALUE")
   }  
}  

Output:

./prog.go:7:13: syntax error: unexpected to, expecting :
./prog.go:9:13: syntax error: unexpected to, expecting :
./prog.go:11:13: syntax error: unexpected to, expecting :
./prog.go:13:13: syntax error: unexpected to, expecting :

Explanation:

The above program will generate syntax errors because we used "to" to compare a range in the switch block, which was not valid.


Program 2:

package main

import "fmt"

func main() {

	switch 30 {
	case 10, 15, 20:
		fmt.Println("Number 10, 15, 20")
	case 25, 30, 35:
		fmt.Println("Number 25, 30, 35")
	case 40, 45, 50:
		fmt.Println("Number 40, 45, 50")
	default:
		fmt.Println("INVALID VALUE")
	}
}

Output:

Number 25, 30, 35

Explanation:

In the above program, we created a switch block and defined 4 cases and one default case. Here "case 25, 30, 35" matched based on given value 30 and printed the "Number 25, 30, 35" on the console screen.


Program 3:

package main

import "fmt"

func main() {

	var num = 12

	switch {
	case num < 10:
		fmt.Println("Less than 10")
	case num < 20:
		fmt.Println("Less than 20")
	case num < 30:
		fmt.Println("Less than 30")
	default:
		fmt.Println("INVALID VALUE")
	}
}

Output:

Less than 20

Explanation:

In the above program, we created a switch block and defined 4 cases and one default case. Here, case num<20 matched based on the value of num that is 12 and printed the "Less than 20" on the console screen.


Program 4:

package main

import "fmt"

func main() {
	var num = 37

	switch {
	case num >= 11 && num <= 20:
		fmt.Println("Between 11 to 20")
	case num >= 21 && num <= 30:
		fmt.Println("Between 21 to 30")
	case num >= 31 && num <= 40:
		fmt.Println("Between 31 to 40")
	default:
		fmt.Println("INVALID VALUE")
	}
}

Output:

Between 31 to 40

Explanation:

In the above program, we created a switch block and defined 4 cases and one default case. Here, "case num>=31 && num<=40 :" matched based on the value of num that is 37 and printed the "Between 31 to 40" on the console screen.


Program 5:

package main

import "fmt"

func main() {
	var num = 37

	switch {
	case num >= 11 && num <= 20:
		fmt.Println("Between 11 to 20")
	case num >= 21 && num <= 30:
		fmt.Println("Between 21 to 30")
	case num >= 31 && num <= 40:
		fmt.Println("Between 31 to 40")
	}
}

Output:

Between 31 to 40

Explanation:

In the above program, we created a switch block and defined 4 cases and one default case. Here, "case num>=31 && num<=40 :" matched based on the value of num that is 37 and printed the "Between 31 to 40" on the console screen.

Note: In Golang default case is not mandatory.

Golang Find Output Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.