Thread: ternary operator

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by itsme86
    Read what Dave posted again. It's very clear. I don't see why you're having problems understanding it:
    Because what Salem linked to seems to contradict Dave. I assumed Dave's explanation was right, but I did not want to throw out Salem's points.

    But the more important question I have is just below that. Namely in what order is the following evaluated "a+b?c-d?e():i():j*k?f():g()".
    Last edited by King Mir; 07-09-2006 at 09:18 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    1) It evalues a+b
    2) If it's true it evalues c-d. Otherwise skip to step 4
    3) If c-d evalues true, it calls e(). Otherwise it calls i(). Done.
    4) It evaluates j*k.
    5) If it's true it calls f(). Otherwise it calls g(). Done.

    It's all very simple...
    If you understand what you're doing, you're not learning anything.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by itsme86
    1) It evalues a+b
    2) If it's true it evalues c-d. Otherwise skip to step 4
    3) If c-d evalues true, it calls e(). Otherwise it calls i(). Done.
    4) It evaluates j*k.
    5) If it's true it calls f(). Otherwise it calls g(). Done.

    It's all very simple...
    And that's what they call reading right to left huh?

    Any reference will tell you that conditionals are evaluated right to left, including some linked earlier in this thread.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #19
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Nobody said anywhere earlier in this thread that conditionals are evaluated right to left except you.
    If you understand what you're doing, you're not learning anything.

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by itsme86
    Nobody said anywhere earlier in this thread that conditionals are evaluated right to left except you.
    Quote Originally Posted by Salem
    More examples here, including a nested ?: expression.
    http://h30097.www3.hp.com/docs/base_...E/DOCU_059.HTM
    READ THE LINK.
    I mean come on, I told you that Dave's post disagreed with Salem's links.


    Besides I can find pleny of other tables just like that one.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You did not read any of them.
    Code:
    ?:  a ? e1 : e2   Expression e1 if a is nonzero; 
                      Expression e2 if a is zero
    You are confusing operator associativity with evaluation order, which is exactly what Salem warned you against.

    Briefly stated, the second link in Salem's post merely warns that in an expression like
    int d = f() - g() * h();
    The results of g() and h() will be multiplied before they are subtracted from the return value of f(). However, the compiler is free to call the functions in any order it wants. This is always true, but when you are controlling program flow the compiler is very rigorous in what happens.

  7. #22
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by citizen
    You did not read any of them.
    Code:
    ?:  a ? e1 : e2   Expression e1 if a is nonzero; 
                      Expression e2 if a is zero
    You are confusing operator associativity with evaluation order, which is exactly what Salem warned you against.

    Briefly stated, the second link in Salem's post merely warns that in an expression like
    int d = f() - g() * h();
    The results of g() and h() will be multiplied before they are subtracted from the return value of f(). However, the compiler is free to call the functions in any order it wants. This is always true, but when you are controlling program flow the compiler is very rigorous in what happens.
    I get all that exept:

    What does it mean by the following?
    In a more complicated example, associativity rules specify that b ? c : d is evaluated first in the following example:

    a ? b ? c : d : e;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, that's actually correct. We all did it backwards, but Dave's post explains how the ternary operator works. Salem's explains how two ternary operators get evaluated if they are in the same expression: the rightmost chunk of logic gets evaluated first, and I am thoroughly, embarrassed.

  9. #24
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I am sure that both Dave's and Salem's post are correct in there own way, I just don't get how they go together.

    I am still confused about the line "a+b?c-d?e():i():j*k?f():g()".

    Perhalps I am the dunce looking for fire today . . . (see my sig)
    Last edited by King Mir; 07-09-2006 at 10:34 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #25
    Registered User
    Join Date
    Jun 2006
    Posts
    12

    ????

    hi everybody..

    i dont know whether the question i posted was clear enough or not... my question still remains unanswered... Let me restate the question..

    I am AWARE that
    Code:
    a<10?b=100:b=200;
    can be written as
    Code:
    b=a<10?100:200;
    but...

    I want a justification as to why
    Code:
    a<10?b=100:b=200;
    is giving an "invalid lvalue" error...

    Wat i am looking for is a "Lexical analysis" of the above statement...
    that is how exactly the compiler is tokenizing the statement.. because the statement is logically correct


    hope that was clear enough...

    Regards
    Abhijith

  11. #26
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by abhijith gopal
    hi everybody..

    i dont know whether the question i posted was clear enough or not... :confused: my question still remains unanswered... Let me restate the question..

    I am AWARE that
    Code:
    a<10?b=100:b=200;
    can be written as
    Code:
    b=a<10?100:200;
    but...

    I want a justification as to why
    Code:
    a<10?b=100:b=200;
    is giving an "invalid lvalue" error...

    Wat i am looking for is a "Lexical analysis" of the above statement...
    that is how exactly the compiler is tokenizing the statement.. because the statement is logically correct


    hope that was clear enough...

    Regards
    Abhijith
    I though you wanted to know what happends with "a<10?b=100:(b=200).

    Any way, in the case of "a<10?b=100:b=200;" it is the same as
    Code:
    (a<10?b=100:b)=200;
    It should be clear why that gives an invalid lvalue.
    Last edited by King Mir; 07-09-2006 at 11:29 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I am sure that both Dave's and Salem's post are correct in there own way, I just don't get how they go together.

    I am still confused about the line "a+b?c-d?e():i():j*k?f():g()".
    While itsme's explaination is easier to understand but goes the wrong way, it is still correct.
    1. Evaluate c - d. if that is nonzero (true) then e() is called if a + b is true. If c - d is zero (false) call i() if a + b is true.
    2. Evaluate a + b. if it is true, then step 1 matters. If it's false, goto 3.
    3. Evaluate j * k. if it is true run f(), otherwise run g().
    Code:
    #include <stdio.h>
    
    int f(void) { return putchar('f'); }
    int g(void) { return putchar('g'); }
    int e(void) { return putchar('e'); }
    int i(void) { return putchar('i'); }
    
    int main(void) {
       int a = 2, b = 3;
       int c = 1, d = -1;
       int j = 0, k = 5;
    
       (a + b)? (c - d)? e() : i() : (j * k)? f() : g();
    
       return 0;
    }
    Playing with that makes it pretty clear.

  13. #28
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    I though you wanted to know what happends with
    Code:
    "a<10?b=100:(b=200).
    Any way, in the case of "a<10?b=100:b=200;" it is the same as

    Code:
    (a<10?b=100:b)=200;

    It should be clear why that gives an invalid lvalue.

    i am not completely convinced with it..
    two doubts

    1. If i am not mistaken the address of "b" is returned & is attempted to be initialized to 200.
    But this error doesnt happen if the staement is true..
    ie., if a<10 is true. in which case b=100 should be evaluated.. the error doesn occur...

    2. correct me if i am wrong..

    this is my interpretation of the code
    (a<10?b=100:b)=200;

    the value of "b" is returned... now since there is an intializing statement the value 200 is equated against the value of b which was returned previously...

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir
    I though you wanted to know what happends with "a<10?b=100b=200).

    Any way, in the case of "a<10?b=100:b=200;" it is the same as
    Code:
    (a<10?b=100:b)=200;
    It should be clear why that gives an invalid lvalue.
    Actually you're wrong. It ends up:

    (a < 10) = ...
    0 = ...
    or
    1 = ...

    That's why it's wrong. Because "a < 10" evaluates to either a 1 or a 0, and you're trying to assign a value to that. Which you can't. Because that's not a valid lvalue.


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

  15. #30
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    Actually you're wrong. It ends up:

    (a < 10) = ...
    0 = ...
    or
    1 = ...

    That's why it's wrong. Because "a < 10" evaluates to either a 1 or a 0, and you're trying to assign a value to that. Which you can't. Because that's not a valid lvalue.


    Quzah.


    thanks.. but... here's one more doubt...

    say a=0

    a<10?b=100:b=200;

    how come this statement works n "b" gets initialized to 100... in this case parenthesis is not required. i tried it out... it seems to mandate the parenthesis for the third operand...

    Regards
    Abhijith

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. ternary operator(?), vectors, and recursive function
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2003, 03:19 PM