Q:

Golang Switch | Find Output Programs | Set 2

belongs to collection: Golang Find Output Programs

0

This section contains the Golang switch find output programs (set 2) with their output and explanations.

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")
   }  
} 

 

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")
	}
}

 

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")
	}
}

 

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")
	}
}

 

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")
	}
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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.


 

Answer Program 2:

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.


 

Answer Program 3:

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.


 

Answer Program 4:

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.


Answer Program 5:

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang goto, break, continue | Find Output Program... >>
<< Golang Switch | Find Output Programs | Set 1...