Logical AND Operator

Dart Logical AND Operator takes two boolean values as operands and returns the result of their logical AND gate operation.

Logical AND Operator returns true if both the operands are true. Therefore, we use AND Operator to check if both the given conditions (operands) return true.

Symbol

&& symbol is used for Logical AND Operator.

Syntax

The syntax for Logical AND Operator is

</>
Copy
operand_1 && operand_2

where operand_1 and operand_2 are boolean values or expressions that evaluate to boolean values.

Truth Table

The truth table of AND operation is shown in the following.

operand_1operand_2operand_1 && operand_2
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Examples

Check if given number is positive and divisible by 5

In the following example, we check if the given integer number n is both positive and divisible by 5. Here there are two conditions. First condition is that if the given number is positive n > 0, and the second condition is that if the number is divisible by 5, n % 5 == 0. And the number must satisfy both these condition, and, therefore we will use Logical AND operator to join these two conditions (n > 0) && (n % 5 == 0).

main.dart

</>
Copy
import 'dart:io';

void main(){
    print('Enter n');
    var n = int.parse(stdin.readLineSync()!);

    if ( (n > 0) && (n % 5 == 0) ) {
        print('$n is positive and divisible by 5.');
    } else {
        print('$n is not positive, not divisible by 5, or both');
    }
}

Output

Enter n
20
20 is positive and divisible by 5.

Output

Enter n
14
14.0 is not positive, not divisible by 5, or both.

Conclusion

In this Dart Tutorial, we learned about Logical AND Operator, its syntax and usage, with examples.