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?
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.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?
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]
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); }
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:
Or another:Code:#include <stdio.h> int main(void) { int Valid; int i = 21; Valid = (i == 21) ? 1 : 0; printf ("%d\n", Valid); return(0); }
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]
thats because what you wrote translates into this: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); }
while the statement 0; is legal, it does nothing.Code:#include <stdio.h> int main(void) { int music = 1; if (music == 1) 0; else 1; printf ("value is %d\n", music); return(0); }
hello, internet!
Ofcourse...Originally posted by Salem
music ^= 1;
is much more succinct
>lambs4:
>>(music == 1) ? 0 : 1
Ofcourse this has no effect it should be:
music = (music == 1) ? 0 : 1
none...
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.Originally posted by Salem
music ^= 1;
is much more succinct
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
It's the same thing...Originally posted by moi
how about music = !music;
A XOR A is equal to !A in this case...
none...
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.Originally posted by ammar
It's the same thing...
A XOR A is equal to !A in this case...
You are right, I was assuming that the only two options are either 0 or 1...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.
I think the Digital Design courses, made me stupid
none...