0
Using the switch function with intervals as case
Is it possible to use intervals to select between the different cases of the switch function in C ? Or can I only use discrete values as case for the switch function?
6 Answers
+ 1
Never knew that before.
https://www.geeksforgeeks.org/using-range-switch-case-cc/
+ 1
Unfortunately, the link does not open at the moment. I don't know if it was mentioned in that article that (as far as I know) case ranges are an extension to the GNU C compiler. It is not standard C, and the use of case ranges may restrict portability of your code, requiring a specific compiler.
Just saying :)
If you're fine with that because it's for you and you always and only use the GNU compiler collection, then go ahead. Just be aware of what you are doing :)
+ 1
Yes it was mentioned in the article.
Nevertheless I tried to use it here in the “Code Playground” section - until now without valuable outcome …
https://code.sololearn.com/c5z00tgG5jEG/?ref=app
I also thought about calculating discrete integers for the case selection by using boolean operations combined within an arithmetic calculation …
0
Whoa thanks for the quick answer. And additionally with positive result! 😊👍🏻
0
Stefan
Thanks for your question!
0
I just wrote some code to calculate an integer for the switch function.
It combines „classic arithmetics“ (multiplication and summation) with boolean operations.
https://code.sololearn.com/cc6n994J5emh/?ref=app
Just the user input dosen‘t work like it should …
So you would have to change the variable user_input directly within the code to see the different behavior.