Skip to content
Related Articles

Related Articles

Golang Program that switch on floating-point numbers
  • Last Updated : 04 May, 2020

With the help of switch case we can implement the functionality of as many if statements. In Golang, switch cases can work with strings, list of variables including integer values as well as floating values.

Syntax:

switch optstatement; optexpression{
case expression1: Statement..
case expression2: Statement..

default: Statement..
}

Example 1: In this example, we can see that by using switch cases and assuming variable as a floating type we can make use of switch cases. Some of the programming languages don’t allow us to use float values but Golang allows us.




// Golang Program that switch
// on floating-point numbers
package main
  
// Here "fmt" is formatted IO which
// is same as C’s printf and scanf.
import "fmt"
  
// Main function
func main() {
    val := 1.1
  
    // Use switch on the day variable.
    switch {
    case val == 1.2:
        fmt.Println("One Point Two")
    case val == 1.3:
        fmt.Println("One Point Three")
    case val == 1.1:
        fmt.Println("One Point One")
    }
}

Output :

One Point One

Example 2:




// Golang Program that switch
// on floating-point numbers
package main
  
// Here "fmt" is formatted IO which
// is same as C’s printf and scanf.
import "fmt"
  
// Main function
func main() {
    gfg := 4.5
  
    // Use switch on the day variable.
    switch {
    case gfg == 1.2:
        fmt.Println("Geek")
    case gfg == 4.5:
        fmt.Println("For")
    case gfg == 5.5:
        fmt.Println("Geeks")
    }
}

Output :

For



My Personal Notes arrow_drop_up
Recommended Articles
Page :