00:00
00:00
Newgrounds Background Image Theme

GasGrass just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Conditional operator (?:)

1,392 Views | 7 Replies
New Topic Respond to this Topic

AS: Conditional operator (?:) 2005-12-30 12:54:38


AS: Main

So I didn't find a tutorial in the AS: Main list about this, so I thought I'd make one.

So, first I'll post an example of two ways you can use the conditinal operator:

Example 1
var a:Number = 5;
a<10 ? trace("a is less than 10") : trace("a is more than 10");
// Output : "a is less than 10"

Example 2
var b:Number = 20;
var c:String = b<10 ? "b is less than 10" : "b is more than 10";
trace(c);
// Output : "b is more than 10"

Now, to begin with the first one, I'll explain it closer. This example might make it more understandable:

condition ? if the condition is true, do this : else do this;

This code is only a shortcut for this:

if(condition){
// if the condition is true, do this
} else {
// else do this
}

In other words, you dont need to know this shorthand, but it sure can be nice to know. You can also nest statements inside each other, like this:

var a:Number = 15;
a<10 ? trace("a is less than 10") : a<20 ? trace("a is between 10 and 20") : trace("a is higher than 20");

This is the same as writing this:

var a:Number = 15;
if(a<10){
trace("a is less than 10");
} else if(a<20){
trace("a is between 10 and 20");
} else {
trace("a is higher than 20");
}

If you don't want any else at the end, simply write:

condition ? if the condition is true, do this : 0;

For the second, it will simply return the value, like this:

condition ? if the condition is true, return this : else return this;

If you have any questions, feel free to ask.


BBS Signature

Response to AS: Conditional operator (?:) 2005-12-30 12:55:29


nice nice, apparently you beat me to it. I was gonna make one about this stuff later... oh well, you explained it better than i would, i think. Great!

Response to AS: Conditional operator (?:) 2005-12-30 12:55:39


Its in "Tricks and tidbits" by GG, as no one saw fit to give it a whole topic of its won (just one operator)

Response to AS: Conditional operator (?:) 2005-12-30 13:00:54


At 12/30/05 12:55 PM, T-H wrote: Its in "Tricks and tidbits" by GG, as no one saw fit to give it a whole topic of its won (just one operator)

besides. there are many things you missed.
like the fact that you cant include certain statements within the operator, such as...

function bob(num:Number):Boolean
num>10 ? return true : null;
return false;
}

bob(10);

that wont work, since the return method cant go inside the conditional. instead you must use either

if (num>10){
return true;
}
return false;

or

return num>10 ? true : false; <-- unnecessary, but its just an example.


BBS Signature

Response to AS: Conditional operator (?:) 2005-12-30 14:36:14


At 12/30/05 01:00 PM, authorblues wrote: if (num>10){
return true;
}
return false;

or

return num>10 ? true : false; <-- unnecessary, but its just an example.

or just

if(num>10) return true;
else return false;

or better yet, just

return num>10;

Response to AS: Conditional operator (?:) 2005-12-30 14:39:42


At 12/30/05 02:36 PM, -dELta- wrote: return num>10;

hey, delta, why dont you try to keep up
i pointed out that the return of a true or false was unnecessary
but i was giving an example of how to do it with the ?:

for a genius, youre a handful of stupid...


BBS Signature

Response to AS: Conditional operator (?:) 2005-12-30 15:49:10


At 12/30/05 02:39 PM, authorblues wrote:
for a genius, youre a handful of stupid...

lol, im tired. anyways i keep getting told i lack in common sense so i guess that has an affect, i actually realised after posting that that thats what you meant, but i couldnt be bothered to wait for the non-double post thingy (lol at 'that that thats')

Response to AS: Conditional operator (?:) 2005-12-30 16:07:24


At 12/30/05 02:39 PM, authorblues wrote:
At 12/30/05 02:36 PM, -dELta- wrote: return num>10;
hey, delta, why dont you try to keep up
i pointed out that the return of a true or false was unnecessary
but i was giving an example of how to do it with the ?:

for a genius, youre a handful of stupid...

</jealousy>


- Matt, Rustyarcade.com