- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program that switch on floating-point numbers
In Go, the switch statement can also be used with floating-point numbers. This feature can be useful in certain scenarios, where a floating-point value needs to be compared against different thresholds or ranges.
In this article, we will go through an example program that demonstrates the use of switch statement on floating-point numbers.
Syntax of Switch Statement with Float
The syntax of switch statement with floating-point numbers is the same as that with any other type. The only difference is that the cases need to be specified as floating-point values.
switch expression { case value1: // code block case value2: // code block default: // code block }
Example
Let's create an example program that takes a floating-point value as input and prints a message based on the value.
package main import ( "fmt" ) func main() { var number float64=1.0 switch number { case 0.0: fmt.Println("The number is zero.") case 1.0: fmt.Println("The number is one.") case 2.0: fmt.Println("The number is two.") case 3.0: fmt.Println("The number is three.") default: fmt.Println("The number is not zero, one, two, or three.") } }
Output
The number is one.
In this program, we first prompt the user to enter a floating-point number using fmt.Scan() function. Then we use a switch statement to compare the input number with different cases.
If the input number is 0.0, we print the message "The number is zero." If the input number is 1.0, we print the message "The number is one." Similarly, for values 2.0 and 3.0, we print "The number is two" and "The number is three" respectively.
Finally, if the input number does not match any of the cases, we print the message "The number is not zero, one, two, or three."
Conclusion
In this article, we learned how to use switch statement on floating-point numbers in Go. We also went through an example program that demonstrates the use of switch statement with floating-point values.