Ternary operators, bad practice?

This is a discussion on Ternary operators, bad practice? within the C++ Programming forums, part of the General Programming Boards category; >>return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4; >explain please, lol if st1 is ...

  1. #16
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    >>return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;
    >explain please, lol

    if st1 is true, return sv1
    else if st2 is true, return sv2
    else if st3 is true, return sv3
    else return sv4

    ... i think
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  2. #17
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    nope try again!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #18
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    >>nope try again!

    eh? what'd i miss?
    Code:
    int myst(bool st1, bool st2, bool st3)
    {
    	return st1 ? 1 : st2 ? 2 : st3 ? 3 : 4;
    }
    
    int myst2(bool st1, bool st2, bool st3)
    {
    	if(st1)
    		return 1;
    	else if(st2)
    		return 2;
    	else if(st3)
    		return 3;
    	else
    		return 4;
    }
    wrote these (which seem to be the original statement, and my crack at it, unless i missed something), and they return the same thing (given the same values).
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  4. #19
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;

    if st1 is true then sv1 is passed to next ? operator and if thats true then sv2 is passed to next ? operator and if thats true sv3 is returned if false sv4 is returned.

    now what happens when one tests false.

    if st1 is false then st2 is passed to next ? op. and if thats false st3 is passed to next ? op and if true sv3 is returned if false sv4 is returned.

    whatever the case the only thing that can be returned is sv3 or sv4.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #20
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Does the ?: operator have any other name then the ternary operator? We don't call ! the unary operator, nor + the binary operator, so why call ?: the ternary operator?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #21
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I think it is know as the ternary conditional operator but everyone knows it as the ternary operator because it is the only one.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #22
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    hmm... i'm not so sure...

    return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;

    if st1 is true, it places sv1 in the place of all that, so you get

    return sv1;

    if st1 is false, it places everything right of the : there, so you get

    return st2? sv2 : st3 ? sv3 : sv4;

    then if st2 is true, it makes that

    return sv2;

    if st2 is false, it makes it

    return st3? sv3 : sv4;

    then you either get

    return sv3;
    or
    return sv4;

    depending on st3.

    i belive this is right, otherwise my function
    Code:
    int myst(bool st1, bool st2, bool st3)
    {
    	return st1 ? 1 : st2 ? 2 : st3 ? 3 : 4;
    }
    would only return 3 or 4, but it can return 1 or 2 (i tested it).
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Stoned_Coder
    return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;

    if st1 is true then sv1 is passed to next ? operator and if thats true then sv2 is passed to next ? operator and if thats true sv3 is returned if false sv4 is returned.
    That is incorrect. Mad Hatter's first explanation is the correct answer.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Around here we just call them terns.

  10. #25
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;

    is this

    if (st1)
    return sv1? sv2:st3 ? sv3:sv4;
    else
    return st2? sv2:st3 ? sv3:sv4;

    ok so far so good. now lets break this down. first top half
    return sv1? sv2:st3 ? sv3:sv4;

    if(sv1)
    return sv2 ? sv3:sv4;
    else
    return st3 ? sv3: sv4;

    see only sv3 or sv4 can be returned.

    now bottom half
    return st2? sv2:st3 ? sv3:sv4;
    is this

    if (st2)
    return sv2 ? sv3:sv4;
    else
    return st3 ? sv3:sv4;

    see again only sv3 or sv4 can be returned.

    Quzah its your example! u surprise me
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #26
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    Originally posted by Stoned_Coder
    return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;

    is this

    if (st1)
    return sv1? sv2:st3 ? sv3:sv4;
    else
    return st2? sv2:st3 ? sv3:sv4;
    no, it isn't. it's this
    Code:
    if(st1)
       return sv1;
    else
       return st2? sv2 : sv3 ? sv3 : sv4;
    simply run the function:
    Code:
    int myst(bool st1, bool st2, bool st3)
    {
    return st1 ? 1 : st2 ? 2 : st3 ? 3 : 4;
    }
    and pass the values (true, true, true) and (false, true, true). you'll see it returns 1 and 2 respectively.
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  12. #27
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    sorry yes you are right. good job i never nest those buggers lol
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #28
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I never use them, thus destroying any confusion. It could be pretty easy to create hard to find bugs in your code with them.

  14. #29
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    >It could be pretty easy to create hard to find bugs in your code with them.

    if you nest them, yes... but they can be more readable and handier than if/else's at times, IMO.
    i would never write code like that which was just discussed.. that was just for fun, i think (and wasn't it just?? )
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  15. #30
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    They're not so bad if you add a few parenthesis for readability.

    return st1 ? sv1 : (st2 ? sv2 : (st3 ? sv3 : sv4));
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 07:37 AM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21