# Ternary Conditional Operator Problem

Page 1 of 1

## 4 Replies - 660 Views - Last Post: 13 January 2013 - 07:59 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=306964&amp;s=d4eb3b7cab28d7effadbcae28292e016&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Cha0sBG

Reputation: 6
• Posts: 167
• Joined: 09-April 09

# Ternary Conditional Operator Problem

Posted 12 January 2013 - 07:07 PM

Alright, so i wanted to make a game of tic tac toe . And i wanted to use ternary conditional operators to ease up things .. the problem is it made my life a hell hole. As much as i think it trough it doesn't add up. So my problem is it always gives the result of array[0][2] . Here is the source so it can make any sense.

```class Board{
public:
Board( )
{
m_GameBoard[0][0] = 'o';
m_GameBoard[0][1] = 'o';
m_GameBoard[0][2] = 'x';
}

int CheckGameStatus( )
{
// 0  ---- o Wins
// 1  ---- x Wins
// -1 ---- continues

//Check first row
int Status = (m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'x') ?  1 : (( m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'o' ) ? 0 : -1);

return Status;

}

private:

private:

char m_GameBoard[3][3];

};
```

Any ways on top of your head to fix this w/o switching from ternary conditional operators ?
PS: sorry for my bad way of explaing things
Thanks

Is This A Good Question/Topic? 0

## Replies To: Ternary Conditional Operator Problem

### #2 Skydiver

• Code herder

Reputation: 4818
• Posts: 15,931
• Joined: 05-May 12

## Re: Ternary Conditional Operator Problem

Posted 12 January 2013 - 07:15 PM

I don't think this code is doing what you think it is doing:
```m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'x'

```

That string of code is checking if m_GameBoard[0][0] is non-zero, and m_GameBoard[0][1] is non-zero, and m_GameBoard[0][2] is 'x'.

### #3 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Ternary Conditional Operator Problem

Posted 12 January 2013 - 07:51 PM

```int Status = (m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'x') ?  1 : (( m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'o' ) ? 0 : -1);

```

In short, don't do this. It doesn't make your code "faster" or more elegant, it just makes it confusing.

Only use the ternary operator when your conditions are readable and the return value is intuitive.

i.e.
```int max = a < b ? b : a;

```

### #4 Cha0sBG

Reputation: 6
• Posts: 167
• Joined: 09-April 09

## Re: Ternary Conditional Operator Problem

Posted 13 January 2013 - 07:15 AM

Alright got it, i'll think of another way. Thanks

### #5 Skydiver

• Code herder

Reputation: 4818
• Posts: 15,931
• Joined: 05-May 12

## Re: Ternary Conditional Operator Problem

Posted 13 January 2013 - 07:59 AM

What problem wheres you trying to solve when you switched to using the ternary operator? What benefit did you think you were getting?

Sent from my T-Mobile G2 using Tapatalk 2