Skip to main content

Overview

Comparison operators (also called relational operators) are used to compare two values. They always return a boolean value: true or false.

Comparison Operators Table

Basic Example

Here’s a complete example demonstrating all comparison operators:
Output:
Comparison operators are essential for control flow statements like if, while, and for loops.

Understanding Equality

Equal (==) vs Not Equal (!=)

In Dart, use == for equality comparison, not =. The single = is the assignment operator!
  • x == 5 checks if x equals 5
  • x = 5 assigns 5 to x

Range Comparisons

Greater Than and Less Than

Inclusive Comparisons

The >= and <= operators include the boundary value:
Use >= and <= when you want to include the boundary value in your comparison. For example, “age must be 18 or older” would use age >= 18.

Practical Examples

Age Verification

Password Length Check

Grade Evaluation

Range Checking