www.thecareerplus.com
The CareerPlus
Home Technical Resources Programming Expressions
Thursday 09th September 2010
 
 
Can we use variables inside a switch statement? Can we use floating point numbers? Can we use expressions?

Discuss it!          


No

The only things that case be used inside a switch statement are constants or enums.
Anything else will give you a
constant expression required error. That is something like this is not valid switch(i) { case 1: // Something; break; case j: // Something; break; } So is this. You cannot switch() on strings switch(i) { case "string1" : // Something; break; case "string2" : // Something; break; } This is valid, however switch(i) { case 1: // Something; break; case 1*2+4: // Something; break; } This is also valid, where t is an enum switch(i) { case 1: // Something; break; case t: // Something; break; } Also note that the default case does not require a break; if and only if its at the
end of the switch() statement. Otherwise, even the default case requires a break;

Discuss it!          

CrackTheIntervew.NET
Advertisement
 
Top! Top!