>>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
Printable View
>>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
nope try again!:D
>>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;
}
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.
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.
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;
}
That is incorrect. Mad Hatter's first explanation is the correct answer.Quote:
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.
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
no, it isn't. it's thisQuote:
Originally 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;
}
sorry yes you are right. good job i never nest those buggers lol
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?? ;))
They're not so bad if you add a few parenthesis for readability.
return st1 ? sv1 : (st2 ? sv2 : (st3 ? sv3 : sv4));