Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Political Peace Party Studios · Sep 03, 2015 at 08:27 AM · c#ifcorrection

Does This Make Sense? Is It Correct?

Hello, thank you for taking the time to read this. I just need to know if this code means what I think it means.

     if (Blah1 == 1 && Blah2 > 2 || Blah1 == 1 && Blah2 == 1) {

     // Do Something.

   }

So I am hoping that this means "If Blah1 is equal to 1 and Blah2 is greater then 2 then do this code OR IF Blah 1 is equal to 1 and Blah2 is equal to 1 then do this code." Is this what this means?

Thanks in advance!

Comment
Add comment · Show 4
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 MewEight · Sep 03, 2015 at 08:35 AM 1
Share

no it doesnt, what it means now is

"if Blah1 is equal to 1 and Blah2 is greater then 2 or Blah1 is qual to 1 and Blah 2 is equal to 1" then do something.

In your description what you are hoping for is

 if((Blah1 == 1 && Blah2 > 2) || (Blah1 == 1 && Blah2 == 2))
 {
   // Do something
 }
avatar image Political Peace Party Studios · Sep 03, 2015 at 08:46 AM 0
Share

$$anonymous$$ewEight so the code you showed me is what I should be doing for it to mean what I was hoping for it to mean correct? If so, please re-post it as an answer so that I can accept it as correct, and reward you a couple reputation points. Thank you!

avatar image MewEight · Sep 03, 2015 at 09:29 AM 0
Share

its totally fine, just trying to help

avatar image Political Peace Party Studios · Sep 03, 2015 at 09:52 AM 0
Share

Alright, thank you for the answer.

2 Replies

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

Answer by pudd1nG · Sep 03, 2015 at 08:36 AM

No, it wont. If I'm reading this correctly you're checking if Blah2 is 1 or higher than 2. You can do this instead.

 if (Blah1 == 1 && Blah2 != 2)


If you really need those 2 specific checks, you'll want to encapsulate them in brackets.

  if((Blah1 == 1 && Blah2 > 2) || (Blah1 == 1 && Blah2 == 2))

Hope that's helpful.

Comment
Add comment · Show 5 · 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 Political Peace Party Studios · Sep 03, 2015 at 08:47 AM 0
Share

Hmm, I don't believe this is what I am looking for. Thanks for the quick reply pudd1ng!

avatar image pudd1nG · Sep 03, 2015 at 08:50 AM 0
Share

It's what your code is doing, essentially. Since checking for 1 or greater than 2 is convoluted way to checking that the value is not 2.

If this needs to be that way because of other factors you can stick with my second recommendation which will work.

avatar image Political Peace Party Studios · Sep 03, 2015 at 08:54 AM 0
Share

Hey thanks pudd1ng since that is pretty much what $$anonymous$$ewEight wrote, out of respect I would like to wait and see if he replies his comment as an answer. If by tomorrow afternoon he does not reply, I will accept your answer as correct. Hope that is ok with you. Again thanks for the help.

avatar image Eno-Khaon · Sep 03, 2015 at 10:16 AM 1
Share

Depending on how you want it sorted, this can also be simplified or extended into multiple segments.

Because you're checking for "Blah1" to be equal to 1 in either case, you can do either:

 if(Blah1 == 1 && (Blah2 == 1 || Blah2 > 2))

or

 if(Blah1 == 1)
 {
     if(Blah2 == 1 || Blah2 > 2)
     {
         // Do something
     }
 }

The second version is based around the idea that "Blah1" won't always be equal to 1. If that's the case, then it doesn't matter what the state of "Blah2" is without the first case being true, so the whole thing will be skipped over with $$anonymous$$imal effort.

avatar image Political Peace Party Studios · Sep 03, 2015 at 10:42 AM 0
Share

Hey Eno $$anonymous$$haon thanks for that comment. Its very informative and helped me understand this even better.

avatar image
2

Answer by Baste · Sep 03, 2015 at 09:05 AM

Binary operations are defined by precedence rules - the operator that has higher precedence is resolved first. && has a higher precedence than ||, so in your case:

 x && y || z && w
 is the same thing as:
 (x && y) || (z && w)

This works the same way with addition and multiplication:

 1 * 2 + 3 * 4
 is the same thing as:
 (1 * 2) + (3 * 4)

This means that your code is doing what you think it's doing. On the other hand, though:

 x || y && z || w
 is the same thing as:
 x || (y && z) || w
 which is resolved left-to-right:
 (x || (y && z)) || w

So what operators you're using influences what order things are resolved in.

As a general rule, though, don't rely on the precedence - put your own () in to make it clear what you are trying to do.

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

30 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

If statement question 3 Answers

play an audio clip in an if statement 2 Answers

C# Check for boolean value with while loop in Update function 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