In switch statements we have to take only constant values if yes than why and if no than why not please explain in c ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In switch statements we have to take only constant values if yes than why and if no than why not please explain in c ?

1st Nov 2018, 11:51 AM
Lata Mishra
Lata Mishra - avatar
4 Answers
+ 2
in java you can use switch with any variable. the case - blocks have to compare to a fix value, because that's exactly what you are trying to do with switch - statements. yet there is no need to use constants here. this concept should be explained in most courses on SoloLearn, because many languages implement it. if you have any specific questions feel free to ask.
1st Nov 2018, 11:59 AM
Mau
Mau - avatar
+ 2
Yep, the cases have to be compile-time constants: that's because switches aren't if-then-else. The compiler does a binary search for the integer in a static lookup table.
1st Nov 2018, 12:10 PM
Vlad Serbu
Vlad Serbu - avatar
+ 2
sorry I must have read over that
1st Nov 2018, 5:21 PM
Mau
Mau - avatar
0
Thanks for answering but i am talking about c programing here.
1st Nov 2018, 12:01 PM
Lata Mishra
Lata Mishra - avatar