- Dart Programming - Home
- Dart Programming - Overview
- Dart Programming - Environment
- Dart Programming - Syntax
- Dart Programming - Data Types
- Dart Programming - Variables
- Dart Programming - Operators
- Dart Programming - Loops
- Dart Programming - Decision Making
- Dart Programming - Numbers
- Dart Programming - String
- Dart Programming - Boolean
- Dart Programming - Lists
- Dart Programming - List Operations
- Dart Programming - Map
- Dart Programming - Symbol
- Dart Programming - Runes
- Dart Programming - Enumeration
- Dart Programming - Functions
- Dart Programming - Interfaces
- Dart Programming - Classes
- Dart Programming - Object
- Dart Programming - Collection
- Dart Programming - Generics
- Dart Programming - Packages
- Dart Programming - Exceptions
- Dart Programming - Debugging
- Dart Programming - Typedef
- Dart Programming - Libraries
- Dart Programming - Async
- Dart Programming - Concurrency
- Dart Programming - Unit Testing
- Dart Programming - HTML DOM
Dart Programming Useful Resources
Dart Programming - Logical Operators
Logical Operators
Logical operators are used to combine two or more conditions. Logical operators return a Boolean value. Assume the value of variable A is 10 and B is 20.
| Operator | Description | Example |
|---|---|---|
| && | And − The operator returns true only if all the expressions specified return true |
(A > 10 && B > 10) is False. |
| || | OR − The operator returns true if at least one of the expressions specified return true |
(A > 10 || B > 10) is True. |
| ! | NOT − The operator returns the inverse of the expressions result. For E.g.: !(7>5) returns false |
!(A > 10) is True. |
Note − Same logic applies to Bitwise operators, so they will become <=, >=, |= and ^=.
Usage of Logical Operators
Example
The following example shows how you can use Logical Operators in Dart −
void main() {
var a = 10;
var b = 12;
var res = (a<b)&&(b>10);
print(res);
}
Output
It will produce the following output −
true
Example
Lets take another example −
void main() {
var a = 10;
var b = 12;
var res = (a>b)||(b<10);
print(res);
var res1 =!(a==b);
print(res1);
}
Output
It will produce the following output −
false true
Short-circuit Operators (&& and ||)
The && and || operators are used to combine expressions. The && operator returns true only when both the conditions return true.
Let us consider the following expression −
var a = 10 var result = (a<10 && a>5)
In the above example, a<10 and a>5 are two expressions combined by an && operator. Here, the first expression returns false. However, the && operator requires both the expressions to return true. So, the operator skips the second expression.
The || operator returns true if one of the expressions returns true. For example −
var a = 10 var result = ( a>5 || a<10)
In the above snippet, two expressions a>5 and a<10 are combined by a || operator. Here, the first expression returns true. Since, the first expression returns true, the || operator skips the subsequent expression and returns true.
Due to this behavior of the && and || operator, they are called as short-circuit operators.