Logical Operators
This class of operators contain those operators which are used to logically combine two or more conditions of the operands. It goes like this:
Operator Symbol | Operator Name | Operator Description |
---|---|---|
&& | And Operator | Use to add two conditions and if both are true than it will return true. |
|| | Or Operator | Use to add two conditions and if even one of them is true than it will return true. |
! | Not Operator | It is use to reverse the result. |
Example 1: Using Logical Operators in the program
void main()
{
int a = 5;
int b = 7;
// Using And Operator
bool c = a > 10 && b < 10;
print(c);
// Using Or Operator
bool d = a > 10 || b < 10;
print(d);
// Using Not Operator
bool e = !(a > 10);
print(e);
}
Output:
false
true
true
Note: Logical operator can only be application to boolean expression and in dart, non-zero numbers are not considered as true and zero as false
Example 2: (Incorrect Way)
void main()
{
int a = 5;
int b = 7;
// Using And Operator
print(a && b);
// Using Or Operator
print(a || b);
// Using Not Operator
print(!a);
}
Output:
Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
Example 3: (Correct Way)
// Dart Program to demonstrate use of
// Logical Operators
void main()
{
var a = true;
var b = false;
// Printing the Values of a & b
print("a: $a , b: $b\n");
// Using And Operator
print("a && b = ${a&&b}");
// Using Or Operator
print("a || b = ${a||b}");
// Using Not Operator
print("!a = ${!a}");
}
Output:
a: true , b: false
a && b = false
a || b = true
!a = false
Operators in Dart
The operators are special symbols that are used to carry out certain operations on the operands. The Dart has numerous built-in operators which can be used to carry out different functions, for example, ‘+’ is used to add two operands. Operators are meant to carry operations on one or two operands.
Contact Us