Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Switch Case

This topic is 5833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!