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 Caiuse · Dec 05, 2011 at 11:08 AM · syntax

Are there any draw backs to using this kind of Switch statement?

Basically I discovered that I'm able to use switch statements to compare values other than just "==". I wondered as I've not seen this technique being used very widely are there any obvious draw backs to this approach, or is this a completely legitimate way of switching through value comparisons.

 var value : int = 50;
 var isWorking : boolean = true;
 
 switch(isWorking){ //can be anything boolean
         case (value > 10) : 
                 //Do Stuff
         break;
         case (value > 5) : 
                 //Do Stuff
         break;
         default :
                 //Do Default Stuff 
         break;
  }
Comment
Add comment
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

2 Replies

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

Answer by Bunny83 · Dec 05, 2011 at 11:53 AM

First of all i don't see the reason why you would do this. Isn't it much clearer and shorter this way:

var value : int = 50;

if (value > 10) {

} else if (value > 5) {

} else {

}

AFAIK the switch statement only works with constant expressions. I guess the compiler evaluates "(value > 10)" to "true" (because value is 50) so it won't work when you change the value at runtime. Beside that the case expressions have to be unique. You can't have two case statements with the same value. You use a boolean so there are only two possible values: "true" and "false". Your two first cases evaluates to true, so what case would you expect to run? The first? the second? both?

You should write code in a way that's clear and easy to read / understand. Even when it works in some way, most other people that have to work with your code don't have a clue what you want to do here.

edit

I've tested it. It seems that it works exactly like my if else chain in UnityScript. That means when you exchange your two case statements (value > 5) before (value > 10) it won't work. case statements usually don't have to be in an explicit order this "special" case requires it.

Comment
Add comment · Show 4 · 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 Caiuse · Dec 05, 2011 at 12:04 PM 0
Share

Okay a good reason as any is readability. Thanks for the feedback, and explanation.

avatar image Caiuse · Dec 05, 2011 at 12:08 PM 0
Share

Just to add, it does work when changing the value at runtime, in my own I have a value changing on update, and i still get the desired output.

edit (in response to your edit):

Obviously if the value is 50 then the first case will break the switch... almost the same as an IF/ELSE

avatar image Statement · Dec 05, 2011 at 12:13 PM 0
Share

Yes, it should work through lambdas. I take it your hint is if there is any performance considerations? You'd probably need a few ten thousand objects to even start noticing differences, if there are any. Then again, to be sure you could make a stress loop that go through a few million tests each frame.

avatar image Bunny83 · Dec 05, 2011 at 07:48 PM 1
Share

There is absolutely no difference in the generated code except the extra (useless) boolean comparison each if statement. The compiler actually convert this into an if else chain, but in C sharp the case values have to be a constant expression. Your example won't compile in C sharp (i've tried it).

avatar image
0

Answer by ks13 · Dec 05, 2011 at 11:21 AM

Hmm, from what i know of "switch", you're doing it wrong. In switch(true), the true should be the vriable to be compared to different cases, so in your example, you'd have case(true), case(false) and eventually default. If you want to compare value, then you should have switch(value).

Comment
Add comment · Show 3 · 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 Caiuse · Dec 05, 2011 at 11:26 AM 1
Share

Well when comparing values directly "==" you would be 100% correct.

But i found this does seem to work, I think whats happrening is if the switch input is "true" (which will be compared at the start of the statement) then calculate if "value > 10".

either way this works, I get the results I'm after... although I've never seen it used it this way so I was wondering if there was any obvious drawbacks?

avatar image syclamoth · Dec 05, 2011 at 11:30 AM 1
Share

Well I for one think it's a very creative use of the switch statement, and there's absolutely no reason why it shouldn't work!

avatar image Statement · Dec 05, 2011 at 03:17 PM 0
Share

@syclamoth, Creative? Yes, maybe. But it is also a bit unorthodox and can cause some serious wtf moments during a code review. I would be careful about doing too dodgy stuff even if it does work. Related topic: http://stackoverflow.com/questions/594135/whats-up-with-static-cast-with-multiple-arguments - even if for(int i = 0; ++i, i < 10;) is valid code, is it still valid code? :)

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

6 People are following this question.

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

Related Questions

Unity JS Syntax Coloring for MonoDevelop 0 Answers

Longest Common Subsequence Recursive Function Error. 1 Answer

Item Scripting Error 2 Answers

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

Trying to Sort a Multi Dimensional Array 1 Answer


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