Using switch(variable) instead of if statements?
Posted: Wed Aug 01, 2012 11:56 pm
Is it at all bad to do something like this:
In this there is no actual switching of the variable state, but it still acts as if statements, saying if case 0 (if state==0) run this code. What if I want to have this format, but I don't want to actual change the value of the variable. Is it bad to do this? I realize this isn't switching the variable, but it provides a nice way to prevent a lot of if statements, and it looks nicer to me. Is there some other way to have this format of case 0, case 1, case 2 etc. rather than if(state==0), if(state==1), if(state==2)
- Code: Select all
switch(state)
{
case 0:
r=255;
break;
case 1:
r=125;
break;
case 2:
r=0;
break;
}
In this there is no actual switching of the variable state, but it still acts as if statements, saying if case 0 (if state==0) run this code. What if I want to have this format, but I don't want to actual change the value of the variable. Is it bad to do this? I realize this isn't switching the variable, but it provides a nice way to prevent a lot of if statements, and it looks nicer to me. Is there some other way to have this format of case 0, case 1, case 2 etc. rather than if(state==0), if(state==1), if(state==2)