ternary operation with compound instruction

This is a discussion on ternary operation with compound instruction within the C Programming forums, part of the General Programming Boards category; Hello guys. By the specification of C I know i can use several instructions (with ) instead of use just ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    6

    ternary operation with compound instruction

    Hello guys.
    By the specification of C I know i can use several instructions (with ) instead of use just one. But is it true if iīm using a ternary operation?
    i.e.,
    Code:
    x > 10 ? printf( "x > 10\n" ) : printf( "x <= 10\n" ); //valid
    x > 10 ? { printf( "x > 10" ); printf( "\n" ); } : { printf( "x <= 10" ); printf( "\n" ) };//question, something like this
    Can you help me (I know my example can be changed in a better way, but itīs just an example!)?

    Thanks, Daniel.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose you could use the comma operator, but you have to know that it only returns the value of the rightmost expression.
    Code:
    x > 10 ? printf( "x > 10" ), printf( "\n" ) : printf( "x <= 10" ), printf( "\n" );
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    6
    Thanks, but it&#180;s wrong. This way it returned the rightmost expression and returned both other expression inconditionally.

    Someone else?

    Thanks.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, may-be you'll need an extra set of parenthesis?

    Code:
    #include <stdio.h>
    int main()
    {
        int a = 5;
        int b = a > 4 ? (printf("More than 4\n"), 1) : (printf("Not more than 4\n"), 2);
        printf("&#37;d", b);
        return 0;
    }
    Anyway, it might be actually clearer as a if...else expression.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A passing thought on the matter:
    Code:
    #include <stdio.h>
    
    void foo(int x, int y)
    {
       printf("&#37;d %s %d\n", x, x < y ? "<" : ">=", y);
    }
    
    int main ()
    {
       foo(5, 10);
       foo(15, 10);
        return 0;
    }
    
    /* my output
    5 < 10
    15 >= 10
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    6
    thanks. itīs good! but itīs sounds strange, doesnīt? it might be
    Code:
    { ... }
    , hence "where thereīs 1 instruction you can put 1 or more in that place."...

    Thanks

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Braces are for code blocks with their own scope. I don't believe any operator (including ? allows it's operands to be blocks of code.

    But there is such a comma operator and it must have some use.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2008, 07:15 PM
  2. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 09:31 PM
  3. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. ternary operation
    By lambs4 in forum C Programming
    Replies: 10
    Last Post: 12-30-2002, 07:29 AM

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