# ternary operators

Page 1 of 1

## 8 Replies - 1468 Views - Last Post: 01 April 2011 - 09:46 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=225460&amp;s=809defdc776879874ca712d1599fd011&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ravi6389

• New D.I.C Head

Reputation: -1
• Posts: 3
• Joined: 31-March 11

# ternary operators

Posted 31 March 2011 - 09:55 PM

can someone please interpret it in form of if else??

cond1?cond2?cond3:exp1:exp2:exp3
Is This A Good Question/Topic? 0

## Replies To: ternary operators

### #2 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: ternary operators

Posted 31 March 2011 - 10:10 PM

That's not how it works. Its:

cond ? exp1 : exp2

and it means

```if (cond)
return exp1;
else
return exp2;

```

where cond is any expression that can be evaluated to just 0 or 1 (false or true).

### #3 ravi6389

• New D.I.C Head

Reputation: -1
• Posts: 3
• Joined: 31-March 11

## Re: ternary operators

Posted 01 April 2011 - 12:25 AM

r.stiltskin, on 31 March 2011 - 10:10 PM, said:

That's not how it works. Its:

cond ? exp1 : exp2

and it means

```if (cond)
return exp1;
else
return exp2;

```

where cond is any expression that can be evaluated to just 0 or 1 (false or true).

Thanx for your reply Stiltskin but the basics of ternary operator which you have so nicely explained is already known to me..i just came across the question somewhere on net and cudnt crack it.hence asked..

### #4 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

## Re: ternary operators

Posted 01 April 2011 - 05:29 AM

ravi6389, on 01 April 2011 - 10:25 AM, said:

can someone please interpret it in form of if else??

cond1?cond2?cond3:exp1:exp2:exp3

What you have is called nested ternary op statement(its not recommended). Its read the same like nested if-else. But the one you have above is wrong. It includes an extra :

### #5 ravi6389

• New D.I.C Head

Reputation: -1
• Posts: 3
• Joined: 31-March 11

## Re: ternary operators

Posted 01 April 2011 - 05:46 AM

chinchang, on 01 April 2011 - 05:29 AM, said:

ravi6389, on 01 April 2011 - 10:25 AM, said:

can someone please interpret it in form of if else??

cond1?cond2?cond3:exp1:exp2:exp3

What you have is called nested ternary op statement(its not recommended). Its read the same like nested if-else. But the one you have above is wrong. It includes an extra :

ok..so can you please tell the solution...

### #6 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

## Re: ternary operators

Posted 01 April 2011 - 05:57 AM

Correct statement would be :
```cond1?cond2?exp1:exp2:exp3
```

This would be equivalent to :
```if(cond1){
if(cond2){
exp1;
}
else{
exp2;
}
}
else{
exp3;
}
```

### #7 shintetsu_80

Reputation: 32
• Posts: 108
• Joined: 01-July 08

## Re: ternary operators

Posted 01 April 2011 - 05:58 AM

Here's and example you can play with to help yourself understand it.

```#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {

bool a = true;
bool b = true;
bool c = true;

string exp = a ? (b ? (c ? "abc" : "ab") : "a") : "f";
cout << "exp: " << exp << endl;

cout << "exp: ";
if (a) {
cout << "a";
if (B)/> {
cout << "b";
if (c) {
cout << "c";
}
}
}
else {
cout << "f";
}
cout << endl;
return 0;
}

```

This post has been edited by shintetsu_80: 01 April 2011 - 06:21 AM

### #8 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: ternary operators

Posted 01 April 2011 - 08:52 AM

As the others have pointed out, the number of results has to be 1 more than the number of conditions.

shintetsu_80's example is good but maybe a little trickier than it needs to be because of the multiple printing statements.

Here's an example that just uses a simple assignment:
```#include <stdio.h>

typedef enum {FALSE, TRUE} bool;

char *eval( bool cond, char *first, char *second ) {
if( cond == TRUE )
return first;
else
return second;
}

int main() {
int cond1 = TRUE, cond2 = FALSE, cond3 = TRUE;
char *str1 = "first", *str2 = "second", *str3 = "third", *str4 = "fourth";
char *result;

if( cond1 == TRUE ) {
if( cond2 == TRUE ) {
if( cond3 == TRUE ) {
result = str1;
} else {
result = str2;
}
} else {
result = str3;
}
} else {
result = str4;
}

printf( "%s\n", result );

//    cond1 = FALSE;
//    cond3 = FALSE;
result = cond1 ? cond2 ? cond3 ? str1 : str2 : str3 : str4;
printf( "%s\n", result );

return 0;
}

```

Notice that if cond2 is FALSE the value of cond3 is irrelevant -- it isn't even tested.

And similarly if cond1 is FALSE the values of both cond2 and cond3 are irrelevant.

• Saucy!

Reputation: 6236
• Posts: 24,001
• Joined: 23-August 08

## Re: ternary operators

Posted 01 April 2011 - 09:46 AM

Don't spoon-feed the help vampires, please!