New course! Every coder should learn Generative AI!
Try a free lesson0
How many maximum cases we can write in switch case?
In C and C++ Language
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)
+ 1
As much as you want, but the code can become a bit unwieldy if you've got very long switch statements
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.
0
You can write as many as you want... Did you finish the course of loops for C++?