Does anyone know what the and/or operator is?
4 Answers
4You are probably asking about && and || operators. 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
How do I enter the || operator without copy-pasting?
that would be xand and xor not or and, and
if your searching for an and/or statement, its best to simply use || since the if statement will work if at least one of those things that you want is achieved.
eg.
if (x == y || a == b)
{
(do whatever)
}
if you use the || operator 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) instead of if( !dead || health > 0) if I want to check whether my character can be hurt....
A question of my own with regards this topic. With (A && B || C) AB = true AC = true BC = false or do I need to have two separate conditions
– Aggelas