Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by husbandofemily · Jul 01, 2013 at 05:32 PM · javascriptifoperator

Shorthand for checking for multiple if's.....

Hi all. Really struggling to word this clearly, so apologies, and bear with me please! I often find myself checking for more than one state of a single variable, for instance

if(aNumber < 5 && aNumber > 1)

That isn't so bad, but with more complicated code it gets very long and sometimes complicated. Is there a way of expressing the "&&" to mean "the last expression again"? so something like

if(aNumber <5 ** >1)

Maybe nothing like this exists, but if it does I'd love to know! (this is in JavaScript btw).

Thanks

Ian

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image InfiniBuzz · Jul 01, 2013 at 05:43 PM 0
Share

you can make a function CheckRange

 function CheckRange(num : int, $$anonymous$$ : int, max : int)
 {
    if(num < max && num > $$anonymous$$)
       return true;
    else
       return false;
 }

and call it like

 if(CheckRange(aNumber, 1, 5))
    // Do ittt :)

you can create overloads for different types

but if this makes it more simple is a different thing ;)

avatar image husbandofemily · Jul 01, 2013 at 05:50 PM 0
Share

InfiniBuzz - I didn't know that, so thanks, I've learnt something! It's not quite what I was after $$anonymous$$d, I was hoping there was an operator to mean "as well as" for the variable. Thanks anyway!

avatar image InfiniBuzz · Jul 01, 2013 at 05:57 PM 0
Share

I know its not directly an answer ;) depending on what you are doing you could also create a more complicated check function that maybe returns an enum you can build a statemachine from (switch-case) - just brainstor$$anonymous$$g ;)

avatar image husbandofemily · Jul 01, 2013 at 06:02 PM 0
Share

Ha, I don't need anything that complicated just yet, but I'll keep that in $$anonymous$$d. That checkrange function might come in handy actually. Cheers :-)

avatar image BlueRaja_2014 · Jul 01, 2013 at 06:43 PM 2
Share

@InfiniBuzz

 if(something)
     return true;
 else
     return false;

can be written more simply/clearly as

 return (something);

Also note that Unity already has a Clamp() method, though unlike yours, it is inclusive.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Owen-Reynolds · Jul 01, 2013 at 06:14 PM

No -- every compare needs both sides. But this is the standard slicing if from any good intro Computer Science textbook. This looks up the chart "-32:ice ; 32-211:water ; 212-999:steam ; 1000+ megasteam" using ifs:

 if(t<32) g="ice";
 else if(t<212) g="water"; // 32-211
 else if(t<1000) g="steam"; // 212-1000
 else g="megasteam";

The trick is general is to always work your way either up or down the range. Notice how each breakpoint (32, 212) is only listed once.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Eric5h5 · Jul 01, 2013 at 06:07 PM

Nothing like that exists. If your code is getting long and complicated, that usually suggests there's a better approach in general, rather than trying to use shortcuts (which make code difficult to read if they do exist).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If statement not working 3 Answers

Check if is destroyed 0 Answers

Overload Operator in UnityScript? 0 Answers

Is there a modulus operator in Unity JavaScript? 4 Answers

if ((this || that) && (that || this)).. Does that work? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges