8 Replies - 1092 Views - Last Post: 01 April 2011 - 09:46 AM Rate Topic: -----

#1 ravi6389  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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).
Was This Post Helpful? 1
  • +
  • -

#3 ravi6389  Icon User is offline

  • New D.I.C Head

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

Re: ternary operators

Posted 01 April 2011 - 12:25 AM

View Postr.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..
Was This Post Helpful? 0
  • +
  • -

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: ternary operators

Posted 01 April 2011 - 05:29 AM

View Postravi6389, 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 :
Was This Post Helpful? 0
  • +
  • -

#5 ravi6389  Icon User is offline

  • New D.I.C Head

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

Re: ternary operators

Posted 01 April 2011 - 05:46 AM

View Postchinchang, on 01 April 2011 - 05:29 AM, said:

View Postravi6389, 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...
Was This Post Helpful? -1
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#7 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: ternary operators

Posted 01 April 2011 - 09:46 AM

Don't spoon-feed the help vampires, please!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1