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.
package main
import "fmt"
func main() {
val := 1.1
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:
package main
import "fmt"
func main() {
gfg := 4.5
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
Share your thoughts in the comments
Please Login to comment...