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 /
  • Help Room /
avatar image
2
Question by Wolfshadow · Mar 25, 2016 at 04:27 PM · c#operatorand-operator

And/Or Operator C#

Does anyone know what the and/or operator is?

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
15
Best Answer

Answer by TobiasW · Mar 25, 2016 at 04:37 PM

You are probably asking about && and ||. You can use them to chain multiple conditions together, e.g.

 if ((health > 50) && hasAmmunition)
 {
    // Attack! (But only if we are healthy AND we have ammunition)
 }

or

 if (raining || snowing)
 {
    // Too wet, will not go outside if it's raining, snowing or both
 }

It's basically:

 false && false == false
 false && true == false
 true && false == false
 true && true == true

and

 false || false == false
 false || true == true
 true || false == true
 true || true == true
Comment
Add comment · Show 8 · 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 FireHawkX · Oct 24, 2016 at 05:48 PM 0
Share

Sorry for reviving this question... your answer was REALLY great! :) and this is by far the thread that is closest to what i wanted to ask... at least it was posted this year ;)

I was searching for a more "advanced" version...

Here is an example : What if you want EITHER condition1 == true OR both condition2 AND condition3 OR both condition2 and condition4

should I use multiple ( ) to separate the || sections? Which of the || or && takes priority?

Finally : Would this work in all cases to represent what i wrote above?

 If (condition1 == true || condition2 == true && condition3 == true || condition2 == true && condition4 == true)
avatar image Zendist FireHawkX · Oct 24, 2016 at 06:04 PM -1
Share

That if statement will short circuit, meaning that if condition1 is true, then the if will run its body (because condition1 is followed by an OR, so it doesn't matter what condition2 has to say). If condition1 is false, and condition2 is true, then it depends on what condition3 is, because if condition3 is also false, then the if body will not be run. And so on. It is left-to-right, and you can use parenthesis to delimit operations to calculate first. Does that make sense? :)

avatar image phxvyper Zendist · Oct 24, 2016 at 08:49 PM 2
Share

@Zendist @FireHawkX You are mostly correct, however... it is important to note that Operators in C# are NOT Left to Right. In fact, the || operator takes precedence over the && operator.

the statement:

 condition1 || condition2 && condition3 || condition2 && condition4

is identical to the statement:

 (((condition1 || condition2) && (condition3 || condition2)) && (condition4))

because the || operator is more anterior than &&, and thus is always the most derived operation.

You can find more about operator precedence and associativity here

Note: Even though there is a precedence of operations in C#, it is still important to note your intention. Using parentheses to explicitly state what you intended for the operation is a good convention to work with.

Show more comments
Show more comments
avatar image TobiasW FireHawkX · Oct 24, 2016 at 08:23 PM 3
Share

should I use multiple ( ) to separate the || sections?

Yes. This is not a competition about using the least amount of parentheses. Always use parentheses to make crystal clear which statements are grouped together. That makes it much more readable.

 if ((condition1 == true) || (condition2 == true && condition3 == true) || (condition2 == true && condition4 == true))

Nobody has to ask whether this will work, right?

Oh, and by the way, you don't need to add "== true". The booleans are already true (or false) by themselves, so:

 if (condition1 || (condition2 && condition3) || (condition2 && condition4))
avatar image FireHawkX TobiasW · Oct 25, 2016 at 11:29 AM 0
Share

again, a HUGE thanks! this is exactly what i will be doing! much easier and clearer! :)

avatar image
-1

Answer by Igorotak · Feb 14, 2018 at 04:52 AM

if your searching for an and/or statement, its best to simply use || since the if statement will work if atleast one of those things that you want is achieved.

eg.

if (x == y || a == b) { (do whatever) }

if you use the "||" statement, it will make sure that what you want to happen will happen because one or both of the conditions are attained.

Comment
Add comment · Show 2 · 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 Hellium · Feb 14, 2018 at 08:00 AM 0
Share

You don't use a conditional operator over an other one because it's simpler to use. You use an operator according to what you want to achieve.

I will use if( !dead && health > 0) ins$$anonymous$$d of if( !dead || health > 0) if I want to check whether my character can be hurt....

avatar image Igorotak Hellium · Feb 20, 2018 at 11:37 AM 0
Share

From what I understand from the guy/ gal's question, he/she is trying to figure out how to activate a command if one or two of the conditions are met. It's why I gave that answer. If you use "&&", then both conditions have to be met for the command to activate, but if you use "||" , then if one or both of them were met, then the command would be activated. Still, I do understand what you're saying.

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

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

Trick to OR Operator 1 Answer

Is it possible to use . Operator to access variables in classes in lists 1 Answer

How do I my operators properly in Unity in c# 0 Answers

Not Checking if Greater than 0 1 Answer

Error CS0023: Operator '!' cannot be applied to operand of type 'UnityEngine.GameObject[]' 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