- Home /
Trying to check "if >" in a switch statement
Hey guys.
Trying to write something like this in a switch.
if(MyInt > 100)
{
doSomething();
}
else if (MyInt > 80)
{
doSomethingElse();
}
So what I did was something like this.
switch (MyInt)
{
case(>100):
doSomething();
break;
}
but as I thought, thats not working. Just checking if someone knows the correct way to write it or if I will have to resort to "if statements".
Thank you.
Answer by Bunny83 · Jun 19, 2011 at 02:14 PM
As far as i know it's not possible with any c-style language (C,C++,C#,Java and of course UnityScript ;) ). The only language i know that supports ranges is Pascal/Delphi but the type is restricted to ordinal types there. I started with Pascal but now i prefer the c version of switch because it's more versatile. If you need to check ranges you have to use an if statement.
The point of switch-case statements is that each case have a unique condition. In Unity it's easier with if () else if() statements.
Answer by Tasarran · Jul 15, 2011 at 08:31 PM
Don't forget, you can use && in an if statement...
if (x < 100 && x > 80)
would trigger if x was between 80 and 100...