And/Or Operator C#
Does anyone know what the and/or operator is?
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
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)
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? :)
@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.
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))
again, a HUGE thanks! this is exactly what i will be doing! much easier and clearer! :)
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.
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....
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
Follow this Question
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