Dart Logical operators

Logical operators only operate on boolean values (true / false) to evaluate whether a condition holds true. They yield exactly two possible results: true (the condition is satisfied) and false (the condition fails).

All conditional checks for if statements, while/for loops, and switch cases in code rely on logical operations under the hood.

There are three core logical operators: AND, OR, and NOT.

OperatorNameDescription
&&Logical ANDReturns true only if both operands are true; supports short-circuit evaluation
||Logical ORReturns true if at least one operand is true; supports short-circuit evaluation
!exprLogical NOT (Negation)Inverts a boolean value: true becomes false, false becomes true

You can draw a parallel between AND/OR logic and the series/parallel circuits you learned in middle school: Logical AND acts like a series circuit, while Logical OR mirrors a parallel circuit.

1. Logical NOT !

Inverts the result of a single boolean expression.

Syntax:

!booleanExpression

Example:

void main() {
  bool isMale = false;
  print(!isMale); // true

  bool flag = true;
  print(!flag); // false
}Code language: PHP (php)

Here, isMale starts as false and flag as true; the NOT operator flips both to their opposite boolean values.

It can also wrap full comparison expressions, as shown below:

void main() {
  int age = 12;
  print(!(age>18)); // true
}Code language: JavaScript (javascript)

The raw comparison age > 18 evaluates to false, and the leading ! negates it to output true.

2. Logical AND &&

ExpressionResult
true && truetrue
true && falsefalse
false && truefalse
false && falsefalse

The result is only true when both operands are true; it returns false if either operand is false.

Short-circuit Behavior: If the left-hand operand evaluates to false, the right-hand code never runs, and the operator immediately returns false.

Think of two switches wired in series: the light bulb only turns on (true) when both switches are closed (true). If either switch is open (false), the bulb stays off (false). This is why the right-hand side is skipped once the left operand is confirmed as false.

void main() {
  bool fun1() {
    print("Function executed!");
    return true;
  }

  bool left = false;
  // Left operand is false, so fun1() never executes
  bool res = left && fun1();
  print("Final result: $res");
}Code language: PHP (php)

Program output:

D:\dartdemo\firstdart>dart run
Building package executable...
Built firstdart:firstdart.
Final result: false
Code language: PHP (php)

Even if fun1() contained errors or exceptions, no crash would occur — the function never runs at all.

Practical Logical AND Example:

void main() {
  String name = 'admin';
  String password = '123321';

  // Both sides must evaluate to true to pass the condition
  if (name=='admin' && password =='456654') {
    print("Login successful");
  } else {
    print("Invalid username or password");
  }
}Code language: JavaScript (javascript)

Program output:

D:\dartdemo\firstdart>dart run
Building package executable...
Built firstdart:firstdart.
Invalid username or passwordCode language: CSS (css)

3. Logical OR ||

Short-circuit Behavior: If the left-hand operand evaluates to true, the right-hand code never runs, and the operator immediately returns true.

ExpressionResult
true || truetrue
true || falsetrue
false || truetrue
false || falsefalse

Sample demonstration:

void main() {
  bool fun1() {
    print("Function executed");
    return true;
  }
  // Left operand is true, triggering short-circuit; fun1() does not run
  bool res = true || fun1();
  print(res); // true
}Code language: PHP (php)

Since the left side of || is true, the fun1() call on the right is completely skipped.

With two parallel switches, the light bulb turns on (true) if at least one switch is closed (true).

void main() {
        bool isParentAgree = true;
	int age = 12;
	if(age>=13 || isParentAgree){
	  print("Entry permitted");
	}
}Code language: JavaScript (javascript)

Summary of Short-Circuit Logic

A && B: If A evaluates to false, return false instantly — B will never be executed or evaluated.

A || B: If A evaluates to true, return true instantly — B will never be executed or evaluated.

This behavior boosts runtime performance and prevents runtime errors, most commonly used for null safety checks.

Null safety example:

String? name;
// Only access .length if name is not null; short-circuit avoids null reference errors
if(name != null && name.length > 0) 
{

}Code language: JavaScript (javascript)

If we tried to access name.length without the preceding null check, the code would crash, as a null variable has no length property. Thanks to short-circuit evaluation, when name is null, the first condition name != null becomes false, and the length check never runs, eliminating the crash risk.

Operator Precedence for Logical Operators

! > && > ||

Like mathematical arithmetic, higher-precedence operators resolve first. Wrap expressions in parentheses () to override default evaluation order.

if (!done && (col == 0 || col == 3)) {
  // ...Do something...
}Code language: JavaScript (javascript)

Step-by-step evaluation order:

  1. Evaluate the parenthesized section first: col == 0 || col == 3 (check if column index is 0 or 3)
  2. Calculate the negation: !done
  3. Combine the two results with the && operator

Full combined demo:

void main() {
  bool done = false;
  int col = 3;

  // Official sample expression
  if (!done && (col == 0 || col == 3)) {
    print("Logic executed: Task incomplete, and column index is 0 or 3");
  }

  // Demonstration of Logical NOT !
  bool isFinish = true;
  print(!isFinish); // false

  // Demonstration of Logical AND &&
  int a = 10, b = 20;
  if (a > 5 && b > 10) {
    print("a is greater than 5 AND b is greater than 10");
  }

  // Demonstration of Logical OR ||
  int score = 55;
  if (score < 60 || score > 90) {
    print("Failing grade OR exceptional high score");
  }

  // Short-circuit behavior showcase
  bool func() {
    print("Function executed");
    return true;
  }
  false && func(); // Left operand false, func() never prints
  true || func(); // Left operand true, func() never prints
}Code language: PHP (php)

Program output:

D:\dartdemo\firstdart>dart run
Building package executable...
Built firstdart:firstdart.
Logic executed: Task incomplete, and column index is 0 or 3
false
a is greater than 5 AND b is greater than 10
Failing grade OR exceptional high scoreCode language: JavaScript (javascript)

Leave a Reply

Your email address will not be published. Required fields are marked *