>>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
>>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?
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
>>nope try again!
eh? what'd i miss?
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).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; }
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?
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
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?
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
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
would only return 3 or 4, but it can return 1 or 2 (i tested it).Code:int myst(bool st1, bool st2, bool st3) { return st1 ? 1 : st2 ? 2 : st3 ? 3 : 4; }
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?
That is incorrect. Mad Hatter's first explanation is the correct answer.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.
Quzah.
Hope is the first step on the road to disappointment.
Around here we just call them terns.
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
no, it isn't. it's thisOriginally 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;
simply run the function:Code:if(st1) return sv1; else return st2? sv2 : sv3 ? sv3 : sv4;
and pass the values (true, true, true) and (false, true, true). you'll see it returns 1 and 2 respectively.Code:int myst(bool st1, bool st2, bool st3) { return st1 ? 1 : st2 ? 2 : st3 ? 3 : 4; }
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?
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
I never use them, thus destroying any confusion. It could be pretty easy to create hard to find bugs in your code with them.
>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?
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