How many maximum cases we can write in switch case? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How many maximum cases we can write in switch case?

In C and C++ Language

9th Feb 2020, 9:50 AM
Avinash Maurya
Avinash Maurya - avatar
4 Answers
+ 3
In practice you can use as many as you need, but there are limits. Atleast memory limit, writing switch statement billions times would require too much memory for the code to be saved. Because you can't write same case twice there is another limit, about how many different primitive values is there. Because eg. 97 and 'a' would be 1 same case, this would mean that we should only care of the values that can store most different values (floats cannot be used). long long can have 2^(8*8) different values. (from -2^63 to 2^63-1) unsigned long long can have 2^(8*8-1) more different values. (from 0 to 2^64-1) 2^(8*8) + 2^(8*8-1) = 2^64 + 2^63 = 2*(2^63) + 2^63 =3*2^63 Switch statement can have 3*2^63 + 1 different cases. (+ 1 comes from the default case)
9th Feb 2020, 10:13 AM
Seb TheS
Seb TheS - avatar
+ 1
As much as you want, but the code can become a bit unwieldy if you've got very long switch statements
9th Feb 2020, 10:13 AM
HNNX 🐿
HNNX 🐿 - avatar
0
You can use it as many as you like, but it is not a good thing, as your code will become long and boring, this can be avoided by using loops, functions, or several other methods.
9th Feb 2020, 10:48 AM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
0
You can write as many as you want... Did you finish the course of loops for C++?
15th Apr 2020, 5:02 AM
SmallNinja2006
SmallNinja2006 - avatar