# The ternary if-else

Page 1 of 1

## 5 Replies - 3475 Views - Last Post: 30 January 2007 - 08:03 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=23375&amp;s=211d6b58c748e392c800b5249ffb16f7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Zammy

• New D.I.C Head

Reputation: 1
• Posts: 48
• Joined: 21-October 06

# The ternary if-else

Posted 30 January 2007 - 06:51 AM

```a = --b ? b : (b = -99);
--b ? b : (b = -99);
```

I am reading a book on c++ so this "Ternary Operator if-else" is explained with these 2 sample statements like examples. Problem is I cannot understand them. Anyone post some examples with explanation will be appreciated.

Evgeni
Is This A Good Question/Topic? 0

## Replies To: The ternary if-else

### #2 Amadeus

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: The ternary if-else

Posted 30 January 2007 - 06:56 AM

http://mathbits.com/...g/operator2.htm

```(a>b) ? (c=25) : (c=45);

```

is the same as
```if (a>b)
{
c=25;
}
else
{
c=45;
}

```

Was This Post Helpful? 0

### #3 Xing

• D.I.C Addict

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: The ternary if-else

Posted 30 January 2007 - 06:58 AM

http://en.wikipedia.org/wiki/%3F:
Was This Post Helpful? 0

### #4 Zammy

• New D.I.C Head

Reputation: 1
• Posts: 48
• Joined: 21-October 06

## Re: The ternary if-else

Posted 30 January 2007 - 07:12 AM

Great help guys, thank you. 8 min later. Really fast respone KUDOS! (next time ill wikepedia it ,though:D)
Was This Post Helpful? 0

### #5 horace

• D.I.C Lover

Reputation: 535
• Posts: 2,825
• Joined: 25-October 06

## Re: The ternary if-else

Posted 30 January 2007 - 07:34 AM

Zammy, on 30 Jan, 2007 - 01:51 PM, said:

```a = --b ? b : (b = -99);
--b ? b : (b = -99);
```

I am reading a book on c++ so this "Ternary Operator if-else" is explained with these 2 sample statements like examples. Problem is I cannot understand them. Anyone post some examples with explanation will be appreciated.

Evgeni

this is one of most convoluted examples of the ? ternary operator I have seen as it also combines -- and assignment in the same expression

```  a = --b ? b : (b = -99)

```

is equivalent to
```  if(--b)
a = b;
else
a = b = -99;

```

(a) if b is 1, --b gives 0 (false) hence b is assigned -99 and this is the value of the expression
(b) if b is not 1, --b gives b = b-1 (true) and this is the value of the expression

the result of the expression is then assigned to a
Was This Post Helpful? 0

### #6 Zammy

• New D.I.C Head

Reputation: 1
• Posts: 48
• Joined: 21-October 06

## Re: The ternary if-else

Posted 30 January 2007 - 08:03 AM

Very stupid example to give then. No idea why that guy tried to make it so complicated. Thank you also.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }