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 FlashX · Dec 01, 2014 at 05:53 AM · if statement

the 'or' operator not working? C#

Hi Guys/Girls,

I'm trying to write a simple if statement with an 'or' in it but can't get it working? What have i done wrong?

   if(myVariable == 15||30||45){ 
 //do something
   }

myVariable is an int so i thought it would just work?

Cheers

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

1 Reply

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

Answer by kdubnz · Dec 01, 2014 at 06:07 AM

Try something like :

  if(myVariable == 15 || 
     myVariable == 30 ||
     myVariable == 45 )
  { 
  //do something
 
  }

Comment
Add comment · Show 15 · 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 Helical · Dec 01, 2014 at 07:18 AM 1
Share

And as an explanation, when you do something like myVariable == 15||30||45 then the C# compiler places brackets by the precedence rules like so

(myVariable) == (15||30||45)

the problem is that when you do 15||30||45 you might get compilation error or an undesired result cause 15 || 30 is not a well defined operation for compilers

avatar image kdubnz · Dec 01, 2014 at 08:12 AM 2
Share

@Helical, Actually, it's a little simpler than that. http://msdn.microsoft.com/en-us/library/6373h346.aspx

The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator deter$$anonymous$$es whether the OR expression as a whole evaluates to true or false.

So you should get an error something like

Operator '||' cannot be applied to operands of type 'int' and 'int'

Added: alt text

conditional error 2014-12-01_18-19-56.jpg (216.5 kB)
avatar image Helical · Dec 01, 2014 at 08:29 AM 1
Share

I know the rules of Oring, i was trying hard to simplify my wording for those who don't program in their sleep.

avatar image kdubnz · Dec 01, 2014 at 08:33 AM 1
Share

@Helical, No dis' intended.

avatar image tanoshimi · May 07, 2017 at 09:55 PM 1
Share

@Hoorza - I can't imagine how that could be undesired behaviour? By definition, as soon as one TRUE condition is found, the result of the OR must be TRUE so the test can return straight away (i.e. "shortcut") without needlessly evaluating all the remaining conditions, which couldn't possibly alter the result.

Show more comments

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

31 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 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

two if() statements with one GetKeyDown won't work. 1 Answer

Or in if statement (not working) 1 Answer

Both sides of my IF statement seem to be firing 2 Answers

what would be the if statement if the previous question loaded is repeated or equal to the same.. 1 Answer

Bool based on objects existing not changing. 2 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