Golang Program that switch on floating-point numbers

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

One Point One

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

For



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.