Public Group

# Simple answer on C++ conditions

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

## Recommended Posts

Could someone tell me a different way of doing this. This was the only way I could get it to work. if((input == 11) || (input == 12) || (input == 13) || (input == 21) || (input == 22) || (input == 23) || (input == 31) || (input == 32) || (input == 33)) It works of course, but I know there has to be better ways. EDIT: Removed source tags

##### Share on other sites
To be honest, I'm not sure what you would want to improve. With short circuiting, it's really not too bad execution wise. It's pretty clear what the code is doing to look at it. There are a handful of things you could do to knock out a few operations, but it would make the code less intuitive.

For instance,
if ((input % 10 >= 1) && (input % 10 <= 3) &&    (input / 10 >= 1) && (input / 10 <= 3))

I would probably just stick with the way you had it originally.

a switch

switch(input)
{
case 21:
case 22:
case 23:
case 28:
break;
}

##### Share on other sites
If the amount of possible numbers is small enough, you can use a table.
A table with for instance 100 elements from 0-99.
Set array positions 11, 12, 13 (and so on) to 1. Then check...

if(array[input]==1)

--------------------------- OR ---------------------------

if( ((input-10)/10<3) && ((input-1)%10<3))

Works only if you use an unsigned for input.

[Edited by - xor on June 8, 2005 3:27:16 AM]

##### Share on other sites
If you made input unsigned (I assume this for is tic-tac-toe) you could use:

if( ( (input / 10) - 1 < 4 ) && ( (input % 10) - 1 < 4 )
{
...
}

Worst case operation list is: 1/, 1%, 2-, 2compares

##### Share on other sites
I wasn't aware that this was for a tic tac toe game, that little piece of info would have helped, good thing ascorbic thought of it.
Btw ascorbic don't you mean <3?

##### Share on other sites
I see what you guys mean for all of your examples. I guess I just didn't like how big it looked. Thanks.

##### Share on other sites
< 3 it is... same difference. :^)

##### Share on other sites
you'd have to rework alot of coe but you could do it like this:

get input as 2 variables and use them as an array index.
const int rowsize=3;
const int colsize=3;

int arr[rowsize * colsize];
int x, y;

if(arr[x + rowsize * y] == 1)

this is all good aslong as x and y are 0 - 2 (representing rows 1- 3 and cols 1-3)

##### Share on other sites

if ( (input >=11 && input =21 && input =31 && input <

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015498
×