Logical OR Operator
Dart Logical OR Operator takes two boolean values as operands and returns the result of their logical OR gate operation.
Logical OR Operator returns true if any of the operands is true. Therefore, we use OR Operator to check if any of the given conditions (operands) is true.
Symbol
||
symbol is used for Logical OR Operator.
Syntax
The syntax for Logical OR Operator is
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 OR operation is shown in the following.
operand_1 | operand_2 | operand_1 || operand_2 |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Examples
Check if given number is even or divisible by 5
In the following example, we check if the given integer number n
is even, or divisible by 5, or both. Here there are two conditions. First condition is that if the given number is even n % 2 == 0
, and the second condition is that if the number is divisible by 5, n % 5 == 0
. The number must satisfy at least one of these two conditions, and, therefore we will use Logical OR operator to join these two conditions (n % 2 == 0) || (n % 5 == 0)
.
main.dart
import 'dart:io';
void main(){
print('Enter n');
var n = int.parse(stdin.readLineSync()!);
if ( (n % 2 == 0) || (n % 5 == 0) ) {
print('$n is either even or divisible by 5, or both.');
} else {
print('$n is neither even, nor divisible by 5.');
}
}
Output
Enter n
12
12 is either even or divisible by 5, or both.
Output
Enter n
25
25 is either even or divisible by 5, or both.
Output
Enter n
13
13 is neither even, nor divisible by 5.
Conclusion
In this Dart Tutorial, we learned about Logical OR Operator, its syntax and usage, with examples.