## Not equals to (number to number)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
brozenadenzen
Prole
Posts: 14
Joined: Tue Feb 14, 2012 5:04 pm

### Not equals to (number to number)

Hi, I was wondering is it possible to make variable comparison to a range of numbers in a simple way, something like if X is not equal to 16 to 20 (which are 16,17,18,19,20) then do something. Would be so nice

Camewel
Prole
Posts: 20
Joined: Thu Apr 19, 2012 5:58 pm

### Re: Not equals to (number to number)

You can just do something like this:

Code: Select all

if x > 16 or x < 20 then
Meaning it will be true as long as it is not between these two numbers. Seems a bit silly to add a special function for this.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

### Re: Not equals to (number to number)

I usually put the two conditions in a way that makes the numbers increase, from lower to upper:

Code: Select all

if min <= x and x <= max then ... 
if you *really* need a function, this should do:

Code: Select all

function between(x,min,max)
return min <= x and x <= max
end
Usage:

Code: Select all

if between(x,16,20) then ...
When I write def I mean function.

mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

### Re: Not equals to (number to number)

Camewel wrote:You can just do something like this:

Code: Select all

if x > 16 or x < 20 then
Meaning it will be true as long as it is not between these two numbers. Seems a bit silly to add a special function for this.
Wont that be trur if x is larger than 16 OR less than 20 meaning that anything would return true
Eg. 5<20 therefore continue if statement
Your screen is very zoomed in...

tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

### Re: Not equals to (number to number)

mickeyjm wrote: Wont that be trur if x is larger than 16 OR less than 20 meaning that anything would return true
Eg. 5<20 therefore continue if statement
True.
Kurosuke needs beta testers

Camewel
Prole
Posts: 20
Joined: Thu Apr 19, 2012 5:58 pm

### Re: Not equals to (number to number)

Oh, you know what I meant.

Code: Select all

if x < 16 or x > 20 then

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

### Re: Not equals to (number to number)

Oh, I read this incorrectly. OP wanted to check that x is not in the range. Well, once you have a function, doing the contrary is easy:

Code: Select all

if not between(x,16,20) then ... 
If you want to write the whole code with no functions, I sill think it helps to have the numbers in order. I'd do something like this:

Code: Select all

if min > x or x > max then ...
But for the negation, I think that "not between" is more clear.
When I write def I mean function.

trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

### Re: Not equals to (number to number)

Actually, what OP specified was not inrange(x, 16, 20) - between(x, 16, 20) implies exclusion of the range limits, 16 and 20.
So :

Code: Select all

function inrange(x, lower, upper)
return x >= lower and x <= upper
end

if not inrange(x, 16, 20) then end
You were closer the first time

### Who is online

Users browsing this forum: pgimeno and 46 guests