Thread: ternary operation

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    ternary operation

    I would like to rewrite this if-else code as a ternary operator:
    if(music == 1)
    music = 0;
    else
    music = 1;

    music = 1 ? 0 : 1
    Is this the correct way?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: ternary operation

    Originally posted by lambs4
    I would like to rewrite this if-else code as a ternary operator:
    if(music == 1)
    music = 0;
    else
    music = 1;

    music = 1 ? 0 : 1
    Is this the correct way?
    Why not try it and see? Anyway, you missed an equals sign.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int music = 1;
        printf ("value is %d\n", (music == 1) ? 0 : 1);
        return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    I did try it first. The compiler says the code has no effect and still does.

    I'm using it like shown in this code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int music = 1;
        (music == 1) ? 0 : 1
        printf ("value is %d\n", music);
        return(0);
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're using it incorrectly. Do you understand what it does? Lets explain a little.

    (TEST) ? MeIfTRUE : MeIfFALSE;
    Looks like this:
    (a == 1) ? 0 : 1;

    Having that line of code on it's own is pointless, as it does nothing. You need to make use of what is returned from the test, else there is no point in having it in there.

    Another working example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int Valid;
        int i = 21;
        
        Valid = (i == 21) ? 1 : 0;
        
        printf ("%d\n", Valid);
        
        return(0);
    }
    Or another:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i = 21;
        
        (i == 21) ? MyFunc() : AnotherFunc();
        
        return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by lambs4
    I did try it first. The compiler says the code has no effect and still does.

    I'm using it like shown in this code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int music = 1;
        (music == 1) ? 0 : 1
        printf ("value is %d\n", music);
        return(0);
    }
    thats because what you wrote translates into this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int music = 1;
        if (music == 1)
          0;
        else
          1;
        printf ("value is %d\n", music);
        return(0);
    }
    while the statement 0; is legal, it does nothing.
    hello, internet!

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Salem
    music ^= 1;
    is much more succinct
    Ofcourse...

    >lambs4:
    >>(music == 1) ? 0 : 1
    Ofcourse this has no effect it should be:
    music = (music == 1) ? 0 : 1
    none...

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Salem
    music ^= 1;
    is much more succinct
    But it doesn't do what the OP wanted. In their post, music was set to 0 if it was 1, and 1 if it was anything else. The XOR method doesn't have that affect.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    how about music = !music;
    hello, internet!

  9. #9
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by moi
    how about music = !music;
    It's the same thing...
    A XOR A is equal to !A in this case...
    none...

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by ammar
    It's the same thing...
    A XOR A is equal to !A in this case...
    No it's not *sigh* Though there is still a similar problem as with Salem's. You only want to change the value to 0 if it's 1, everything else should be 1.

  11. #11
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Polymorphic OOP
    No it's not *sigh* Though there is still a similar problem as with Salem's. You only want to change the value to 0 if it's 1, everything else should be 1.
    You are right, I was assuming that the only two options are either 0 or 1...

    I think the Digital Design courses, made me stupid
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change this simple opeation into power operation..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 12-20-2008, 03:17 PM
  2. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  3. ternary operation with compound instruction
    By byfreak in forum C Programming
    Replies: 6
    Last Post: 07-01-2008, 03:45 AM
  4. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM