Wednesday, Jan 17th

Last update12:59:40 PM GMT

Can we use variables inside a switch statement? Can we use floating point numbers? Can we use expressions?

Write e-mail

No

The only things that case be used inside a switch statement are constants or enums. Anything else will give you a

argaiv1077

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;

Share this post



Web Hosting