Open In App

Golang Program that switch on floating-point numbers

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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


Last Updated : 04 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads