Fill in the blank

Multiple operands in a switch statement

Page 1 of 1

2 Replies - 3153 Views - Last Post: 24 February 2010 - 06:31 AM

#1 Guest_bmart*


Reputation:

Fill in the blank

Posted 22 February 2010 - 03:15 PM

Hi, I want to rewrite an if/else statement with multiple operands as a case statement. The if/else works, but it bugs me that I can't conceptualize this.

/////////////////////// IF/ELSE //////////////////////////////////

if ((pTypes[0]=="L")&&(pTypes[1]=="L")&&(pTypes[2]=="O")) 

{
   do this
}
		
else if ((pTypes[0]=="L")&&(pTypes[1]=="O")&&(pTypes[2]=="O"))

{ 
   do this
}

else if ((pTypes[0]=="L")&&(pTypes[1]=="O")&&(pTypes[2]=="O"))

{ 
   do this
}//etc
	
/////////////////////// SWITCH //////////////////////////////////

switch ( //[b]WHAT GOES HERE?[/b])

{

    case (pTypes[0]=="L")&&(pTypes[1]=="L")&&(pTypes[2]=="O") :

		do this;

		break;

    case (pTypes[0]=="L")&&(pTypes[1]=="O")&&(pTypes[2]=="O") :

		do this;

		break;

    case (pTypes[0]=="L")&&(pTypes[1]=="O")&&(pTypes[2]=="O") :

		do this;

    default:

                do this;


}


Is This A Good Question/Topic? 0

Replies To: Fill in the blank

#2 TheBeege   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 01-April 09

Re: Fill in the blank

Posted 23 February 2010 - 08:58 AM

If it were one variable instead of an array, you could do...

switch(myVar)
{
case(1):
case(2):
// code for both cases 1 and 2
break;
}



As for checking the case of an array's values, everything I could find said to just use if...else, and I have no first-hand knowledge with such a method. Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 Guest_bmart*


Reputation:

Re: Fill in the blank

Posted 24 February 2010 - 06:31 AM

Works for me. Thanks! :)
Was This Post Helpful? 0

Page 1 of 1