Forum Topic: if number = 1 2 or 3

(119 views • 13 replies)

This topic is 1 page long.

<< < > >>
None

Girdf

Reply To Post Reply & Quote

Posted at: 10/23/09 03:15 PM

Girdf EVIL LEVEL 25

Sign-Up: 03/22/05

Posts: 614

if (_root.number==1,6,25,63,90) {_root.asdf=5;}

i know that's not right
but thats pretty much what i want

is there a way i can make the code like that but have it for certain numbers.

like if number = either the number 1, 6, 25, 63, or 90 than asdf would =5


None

Cojones893

Reply To Post Reply & Quote

Posted at: 10/23/09 03:18 PM

Cojones893 EVIL LEVEL 22

Sign-Up: 03/09/03

Posts: 2,596

if(a == 5 && b == 10 && c == 12){}

None

Ahnimal

Reply To Post Reply & Quote

Posted at: 10/23/09 03:21 PM

Ahnimal LIGHT LEVEL 09

Sign-Up: 02/12/09

Posts: 75

Use ||. It basicly means or.
like this:

if(number==1 || number==2 || number ==3){}

remember double = when comparing values.
the above example is easier to write as:

if(number >=1 && number <= 3){}

The universe is made of imbalances


None

Girdf

Reply To Post Reply & Quote

Posted at: 10/23/09 03:21 PM

Girdf EVIL LEVEL 25

Sign-Up: 03/22/05

Posts: 614

At 10/23/09 03:18 PM, Cojones893 wrote: if(a == 5 && b == 10 && c == 12){}

i need it all on the same varible

like

if A=5,6,or 9
then b=10


None

knugen

Reply To Post Reply & Quote

Posted at: 10/23/09 03:23 PM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,685

if (number == 1 ||  number == 90)
{
    _root.asdf = 5;
}

You could also use some other tricks with switch/case statements or arrays to get cleaner code, but this is functional.


None

Girdf

Reply To Post Reply & Quote

Posted at: 10/23/09 03:23 PM

Girdf EVIL LEVEL 25

Sign-Up: 03/22/05

Posts: 614

thanks


None

K-Guare

Reply To Post Reply & Quote

Posted at: 10/23/09 03:27 PM

K-Guare FAB LEVEL 17

Sign-Up: 05/23/08

Posts: 2,427

Well, a disgusting, inefficient option would be
to use a logical OR operator, the ||.
But it'd have to look like this:

if (_root.number==1 || _root.number==6 || _root.number==25 || _root.number==63 || _root.number==90){
     _root.asdf=5;
}

But that's ugly.
A bit of a better way to do this is to stick
all the numbers you want to check its equality to in an Array.
Then use a for...in loop to loop through the values.

var myArr = [0, 5, 16, 64, 27];
for (var a in myArr) {
	if(_root.number == myArr[a]) {
		_root.asdf=5;
		break;
	}
}

Note that after a match was found, I broke out of the loop
so if the array is HUGE it wouldn't keep going if it already found a match.

dr. seuss
"unless someone like you cares a whole awful lot, nothing is going to get better. it's not."

BBS Signature

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/23/09 03:28 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

At 10/23/09 03:23 PM, knugen wrote: if (number == 1 || number == 90)
{
_root.asdf = 5;
}

You could also use some other tricks with switch/case statements or arrays to get cleaner code, but this is functional.
switch(number){
  case 1:
  case 4:
  case 90:
  do stuff;
  break;
  default:
}

is most certainly NOT cleaner code.

When you have a large amount of data to compare a number to, say
var POPTIONS:Array = [1, 4, 3, 8, 45, 278, 65, 83, 24, -67, -22, 30968, -928, -3982756];

wrap it into a function, and call that function like so

function inarray(n:int, a:Array):Boolean{
for(var i:int = 0; i<a.length; i++){
if(n == a[i]) return true;
}
return false;
}

then in your code

if(inarray(54, POPTIONS)){
}

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/23/09 03:35 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

At 10/23/09 03:27 PM, K-Guare wrote: Well, a disgusting, inefficient option would be
to use a logical OR operator, the ||.

FYI that's actually not an inefficient option, any fluff you do on top of that is going to make it slower.

Just imperceptibly slower, so it's not an issue.

(in c++ at least, don't know for sure about flash), || and && "short circuit" when they've reached a point where they can deduce the value of the whole statement

I.E. (excuse the c++ it's been a few months since i've touched flash)

int a = 4;
if(a == 1 || a == 2 || a == 3 || a == 4 || a == 5 || a == 6 || a == 7){
  STUFF
}

It'll evaluate a==1, a==2, a==3, and stop when a==4 returns 1 (because anything || with 1 still returns 1, there's no need to evaluate the other statements).

Similarly with &&, when one statement in a chain is evaluated to 0, it'll stop and continue.

The simple way to test this in flash would be to stick a function call at the end of a chain that should be short circuited early, if the function gets called then FLASH IS STUPID

this tidbit of information is brought to you by the letter F

None

knugen

Reply To Post Reply & Quote

Posted at: 10/23/09 03:37 PM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,685

At 10/23/09 03:28 PM, Glaiel-Gamer wrote: is most certainly NOT cleaner code.

If you say so :) I would prefer it over a long list of conditions, it would be easier to see exactly what numbers you're comparing to, but it isn't really relevant as I would always go with the array solution anyhow.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 10/23/09 03:38 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

It's called lazy evaluation, and yes, Flash does it too.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/23/09 03:44 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

At 10/23/09 03:38 PM, GustTheASGuy wrote: It's called lazy evaluation, and yes, Flash does it too.

It's actually not quite the same thing

http://en.wikipedia.org/wiki/Short-circu it_evaluation
http://en.wikipedia.org/wiki/Lazy_evalua tion


None

K-Guare

Reply To Post Reply & Quote

Posted at: 10/23/09 03:45 PM

K-Guare FAB LEVEL 17

Sign-Up: 05/23/08

Posts: 2,427

At 10/23/09 03:35 PM, Glaiel-Gamer wrote: The simple way to test this in flash would be to stick a function call at the end of a chain that should be short circuited early, if the function gets called then FLASH IS STUPID
var a:Number = 5;

if(a == 1 || a == 5 || a == 7 || a == 2 || a == 9 || Boolean(a)) {
	trace("FLASH IS STUPID");
}

Only traced once, both AS2 and AS3.
Well played, Flash. Well played.

dr. seuss
"unless someone like you cares a whole awful lot, nothing is going to get better. it's not."

BBS Signature

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/23/09 03:47 PM

Glaiel-Gamer NEUTRAL LEVEL 28

Sign-Up: 12/28/04

Posts: 8,069

At 10/23/09 03:45 PM, K-Guare wrote: Only traced once, both AS2 and AS3.
Well played, Flash. Well played.

want more fun?

var a:Boolean = true;
a && somefunction();

//same as

if(a) somefunction();

although don't blame me when people start asking WTF IS YOUR CODE DOING?


All times are Eastern Standard Time (GMT -5) | Current Time: 11:27 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!