- Home /
checking if 2 out of 4 is true no matter which one
Let's say that I have a switch statement like so
     switch(id){
     case 1:
         break;
     case 2:
         break;
     case 3:
         break;
     case 4:
         break;
     }
How can I detect in an easy manner without overloading my script with tons of code wether 2 out of these 4 are true, may it be 1 and 2 or 2 and 3 or 4 and 1, whenever 2 are true I need something to happen how can I achieve very easily and cleanly? (I know how to do it but I want a nice and clean way if possible)
Thanks!
Pleas note overloading is a specific technical term in computer program$$anonymous$$g. What you are doing has no relevance to overloading.
how can your "id" be equal to 2 different value at the same time ?
I think you need to be a little bit more precise about your question... How are you using it ?
Clean code is sometimes more a matter of formatting than approach. With four bool, there are only six states:
 if (   ( a &&  b  && !c  && !d)
     || ( a && !b  &&  c  && !d)
     || ( a && !b  && !c  &&  d)
     || (!a &&  b  &&  c  && !d)
     || (!a &&  b  && !c  &&  d)
     || (!a && !b  &&  c  &&  d)
    
) {
And if the conditions was 'at least 2' rather than 'exactly 2', the condition would be substantially simpler.
Answer by Kiwasi · Aug 27, 2014 at 10:59 AM
Pseudo code
 private bool[] bools = new bool[4];
 private int count = 0;
 
 foreach(bool myBool in bools){
     if(myBool){
         count++;
     }
 }
 if (count>=2){
     return true;
 } else {
     return false;
 }
You can also add them to a list if you want to use named vars ins$$anonymous$$d of an array :)
Answer by Ellandar · Aug 27, 2014 at 11:09 AM
Hi Jan,
I think the problem here is you want something that switch cannot provide. Basically switch is shorthand for an if-elseif block of code, thus your id variable can only be equal to a single case at any run through.
Without much more info, I can only give you a possible solution that may work.
 int trueCounter;
 
 if(id == case1)  trueCounter++;
 if(id == case2)  trueCounter++;
 if(id == case3)  trueCounter++;
 if(id == case4)  trueCounter++;
 
 if(trueCounter == 2)
 {
    // your stuff happens here
 }
Your answer
 
 
             Follow this Question
Related Questions
Operator '==' cannot be used! 2 Answers
Saving drop down menu selection 1 Answer
Switch case using enum javascript 2 Answers
add case in swicth, from editor unity 0 Answers
Why does only one object respond, while more objects have exact same script? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                