Q:

Golang program to demonstrate the switch case without optional statement and expression

0

In this program, we will demonstrate the switch case, here we will not use optional statements and expressions.

All Answers

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

Program/Source Code:

The source code to demonstrate the switch case without an optional statement and expression is given below. The given program is compiled and executed successfully.

// Golang program to demonstrate the switch case 
// without optional statement and expression.

package main
import "fmt"

func main() {
    var month int=3
     switch { 
       case month==1: 
            fmt.Println("JAN") 
       case month==2: 
            fmt.Println("FEB") 
       case month==3: 
            fmt.Println("MAR") 
       case month==4: 
            fmt.Println("APR") 
       case month==5: 
            fmt.Println("MAY") 
       case month==6: 
            fmt.Println("JUN") 
       case month==7: 
            fmt.Println("JUL")
       case month==8: 
            fmt.Println("AUG") 
       case month==9: 
            fmt.Println("SEP") 
       case month==10: 
            fmt.Println("OCT")
       case month==11: 
            fmt.Println("NOV") 
       case month==12: 
            fmt.Println("DEC") 
       default:  
            fmt.Println("Invalid Month") 
   }  
}

Output:

MAR

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we demonstrated the use of a switch case without an optional statement and expression. Here, we used the "==" operator to select the correct case and executed the body for the same.

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now