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?

19th Jan 2022, 11:08 AM
Stefan
6 Answers
19th Jan 2022, 11:27 AM
FanYu
FanYu - avatar
+ 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 :)
19th Jan 2022, 11:51 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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 …
19th Jan 2022, 11:58 AM
Stefan
0
Whoa thanks for the quick answer. And additionally with positive result! 😊👍🏻
19th Jan 2022, 11:33 AM
Stefan
0
Stefan Thanks for your question!
19th Jan 2022, 11:43 AM
FanYu
FanYu - avatar
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.
22nd Feb 2022, 7:48 AM
Stefan