+ 2
and logical operator
What does the and logical operator do?
3 Answers
+ 10
Let's assume you have two varibles a and b. When they are both 0, you want to print 'zero'. When they are both 5, you want to print 'five'. In all other cases, you want to print the sum. You'd code:
if (a == 0 && b == 0)
System.out.println('zero');
else if (a == 5 && b == 5)
System.out.println('five');
else
System.out.println(a+b);
The && is your logical and. Both side's expressions must be true in order for the combined expression to be true. If a is 0 yet b isn't, you output the sum.
+ 4
1. Logical AND Operator
We can combine many relational expressions using āLogical Andā operators .The result will be a boolean type.Ā Ā This operator is represented by the symbol ā&&ā.Ā
Consider the operation āoperand1 && operand2ā
Here operand1 and operand2 can be relational expressions or boolean types. The result of the operation āoperand1 && operand2ā will be
1. ātrueā only if both operand1 and operand2 are ātrueā
2. āfalseāĀ Ā if any of the operands (operand1 or operand2) is āfalseā or both the operands (operand1 or operand2) are āfalseā.
+ 2
Thank you