ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to demonstrate the switch case with optional statement and expression in Golang (Go Language)?
Submitted by Nidhi, on February 20, 2021

Problem Solution:

In this program, we will demonstrate switch case, here we will use optional statement and expression for case selection.

Program/Source Code:

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

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

package main
import "fmt"

func main() {
     switch month:=3; month{ 
       case 1: 
            fmt.Println("JAN") 
       case 2: 
            fmt.Println("FEB") 
       case 3: 
            fmt.Println("MAR") 
       case 4: 
            fmt.Println("APR") 
       case 5: 
            fmt.Println("MAY") 
       case 6: 
            fmt.Println("JUN") 
       case 7: 
            fmt.Println("JUL")
       case 8: 
            fmt.Println("AUG") 
       case 9: 
            fmt.Println("SEP") 
       case 10: 
            fmt.Println("OCT")
       case 11: 
            fmt.Println("NOV") 
       case 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 the switch case.

switch month:=3; month{ 
}

In the above code, month:=3; is an optional statement, here we assigned the value 3 to the month variable, and here variable month is used as an expression for the switch case to select the correct case and execute the body of the correct case.

Golang Switch Statement Programs »



ADVERTISEMENT


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.