CodeBlanks Logo Icon CodeBlanks

Comparison operators

Learn to compare values with ==, !=, <, >, <= and >=.

Comparison operators

Comparison operators compare two values and return a bool (true/false). Six basic operators: ==, !=, <, >, <=, >=.

Important: = is assignment, == is equality comparison. x = 5 assigns 5 to x; x == 5 asks whether x equals 5.

Since the result is a bool, it is used in if conditions: if (age >= 18) { print('Adult'); }.

Practice Print comparisons

Write print(5 == 5), print(3 != 7) and print(4 < 10) inside void main().

Starter code

main.dart
// Comparison operations

Sample solution

main.dart
void main() {
  print(5 == 5);
  print(3 != 7);
  print(4 < 10);
}

Expected output

true
true
true

Practice Compare with condition

Declare var score = 75;. Check with if (score >= 50) and print print('Passed').

Starter code

main.dart
// Compare with a condition

Sample solution

main.dart
void main() {
  var score = 75;
  if (score >= 50) {
    print('Passed');
  }
}

Expected output

Passed

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the difference between = and ==?

  • = assigns, == compares Correct answer
  • They are the same
  • == is faster
  • = only in classes

Explanation: = assigns; == checks equality.

2 Comparison operators

Term Meaning
== Equal
!= Not equal
< Less than
> Greater than

3 Not equal

Question
if (a ____ 5) { print('different'); }
Answer
if (a != 5) { print('different'); }

Explanation: If a is not 5, it returns true.

All lessons in Beginner