Jump to content

if statement to find every combination of 2's/3's/4's against 2's/3's/4's


Fog Juice

Recommended Posts

Hi,

 

I'm in a bit of a pickle. I have three variables, var1, var2, and var3. Each one is randomly assigned a number between 1 and 12. I want to write an if statement that will find out if any of those variables equal 2,3,or 4 all together.

 

So in otherwords, I want to find out if all 3 variables fit one of the following 27 combination:

 

2-2-2

2-2-3

2-2-4

2-3-2

2-4-2

2-3-3

2-3-4

2-4-3

2-4-4

 

3-2-2 

3-2-3

3-2-4

3-3-2

3-3-3

3-3-4

3-4-2

3-4-3

3-4-4

 

4-2-2

4-2-3

4-2-4

4-3-2

4-3-3

4-3-4

4-4-2

4-4-3

4-4-4

 

In words it goes something like "if (var1 is equal to 2, 3, or 4) and (var2 is equal to 2, 3, or 4) and (var3 is equal to 2, 3, or 4) then do this".

 

Unfortunately I cannot do if(($var1 == 2 || 3 || 4) && ($var2 == 2 || 3 || 4) && ($var3 == 2 || 3 || 4)) because it doesn't give proper results.

 

If anyone has any idea how I can do this without having to write 27 if/else if statements to check for each combination, that would be great.

 

Thanks

Link to comment
Share on other sites

If this helps anyone, try to think of it as a 3 reeled slot machine.

 

There is BAR, double BAR, and TRIPLE bar. You win $5 when the reels display 3 x any BAR. So we need to figure out how the computer calculates the combination without doing 27 if statements. I'm doing this for a little slot machine php app and this has been driving me crazy.

Link to comment
Share on other sites

Any single condition in the if must be a valid comparison by itself.

looking at:

if($var1 = 2 || 3 || 4)

if(3) doesn't work

 

if(($var1==2 || $var1==3 || $var1==4) && 
($var2==2 || $var2==3 || $var2==4) && 
($var3==2 || $var3==3 || $var3==4))

 

Remember that && and || are binary operators, hence they must operate on 2 (bi) values.

 

Link to comment
Share on other sites

Any single condition in the if must be a valid comparison by itself.

looking at:

if($var1 = 2 || 3 || 4)

if(3) doesn't work

 

if(($var1==2 || $var1==3 || $var1==4) && 
($var2==2 || $var2==3 || $var2==4) && 
($var3==2 || $var3==3 || $var3==4))

 

Remember that && and || are binary operators, hence they must operate on 2 (bi) values.

 

 

damn it so simple. Thanks :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.