Advertisement

Switch Case

Started by June 01, 2002 10:50 AM
5 comments, last by Pipo DeClown 22 years, 7 months ago
Can someone tell me how to do "if"s in "switch"s?
    
for (int i=0;i<(SCR_H);i++)
		{
		  switch(i)
		  {
		      case 0: array[0]=1;array[1]=1;
			          array[2]=1;array[3]=1;
		              break;
		      case /*if(i>0&&<SCR_H)*/: array[1]=0;array[2]=0;
			          break;
		      case SCR_H: array[0]=1;array[1]=1;
			          array[2]=1;array[3]=1;
		         	  break;
	     	 default: array[0]=1;array[1]=1;
			          array[2]=1;array[3]=1;
		  }
	   cout<<(char)array[a]http://;

		}
    
thnx! [edit]undersign........ [edit]i deleted the CASE 3 part.......... [edited by - Pipo declown on June 2, 2002 3:50:29 AM] [edited by - Pipo declown on June 2, 2002 3:54:23 AM]
You can''t. A the number next to a case must always be a constant value. If you need to check for a condition you should use a if-statement, not a switch.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Advertisement
What does this line mean?:

case /*if(i>0&& break;

And what is it you are trying to do?
Yea, I''m not sure what you''re trying to do either, but you can use if structures in switch...


  switch(x){  case 1:   {     if (z = 2)     {       //do something     }//end if      } break;}//end switch  


It doesn''t look like that''s what you''re trying to accomplish, but here it is if that''s what you''re trying to do
Peon
In C++ you cannot use different boolean expressions in different cases, as you can in basic. The reason is that most C++ compilers will attempt to turn your switch statement into a jump table instead of a bunch of conditional statements. This means that instead of having to go through each case, the computer evaluates the switch statement, and looks up in the jump table where it should go for that value, and goes there. If you have a large switch statement, this is obviously much faster that having to evaluate every comparision until you find the right one, which is what happens with big if else if statements. Anyways, if you want the kind of behavior you are describing, you have to use if else if.
i couldnt make an under sign(points to left)

so it was /*if (i above 0 && i under SCR_H) : blablabla


[edited by - Pipo declown on June 2, 2002 3:56:07 AM]
Advertisement
quote:
i couldnt make an under sign(points to left)

use the [ source ] tag (take out the spaces). click edit on my post to see what i mean.


quote:
so it was /*if (i above 0 && i under SCR_H) : blablabla


Anyways, that you can do, but you have to make multiple case labels and let them all fall though.


  switch (x){    //Same as if ((x >=a && x < e) || (x >= ''A'' && x < ''E''))  case ''a'':  case ''A'':  case ''b'':  case ''B'':  case ''c'':  case ''C'':  case ''d'':  case ''D'':     //do stuff  break;}  

This topic is closed to new replies.

Advertisement