Dart Programming – Logical Operators

  • Post author:
  • Post category:Dart
  • Post comments:2 Comments
Dart Programming logical Operators

In this guide, we will discuss Logical Operators in Dart Programming Language.

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);  
}    

It will produce the following output βˆ’

true 

Example

Let’s 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); 
}    

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.

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply