- Home /
How do you say in between for an if statement?
How would I say:
function Update () { if(transform.position.x "is in between number x and number y") { ( "do stuff" ) } }
Answer by Berenger · Feb 02, 2012 at 11:05 PM
You need to do :
if( transform.position.x >= A && transform.position.x <= B ) ...
What you need to see here :
- A must be lower than B
- if you want an exclusive interval, don't use >= but > etc
- The use of && means that both statement MUST be true.
I'm trying to get it to check between two different X positions, how would I go about this?
Late reply. Info for the future finders I guess.
A = 5; //or whatever lower number
B = 10; //or whatever higher number
if( transform.position.x >= A && transform.position.x <= B ){/*do stuff*/};
This code Berenger gave IS how you would check between the two different x positions, being defined by your variables, A and B.
Right, it may be a bit confusing to name the two values "x" and "y", though he just took that na$$anonymous$$g from the original question. I would never name a limit for a x axis position "y" ^^. Though technically it doesn't matter whatsoever. You can name one "richard" and the other "eth5ju3n2gd".
For a general example it would be better to use generic names like "A" and "B" just like you did. I'm going to edit Berenger's answer so it's less confusing for future readers.
Answer by eagle4 · Feb 02, 2012 at 11:02 PM
not sure if there is a better way, but you could just have nested if statements
if (x<100)
{
if (x>50)
{
print ("we're now in between 50 and 100... woohoo!!");
}
}
Though this works, Berenger's method is much more elegant and widely accepted as the standard.
Yeah I agree, I just didn't know what the syntax for it was.
Your answer
Follow this Question
Related Questions
If \ Else how does the program reads it? 2 Answers
|| conditional statement not working 1 Answer
How to make an if statement with two conditions? 1 Answer
String Scanning Switch Statement 1 Answer