Menu
Coddy logo textTech

Comparison Operators

Part of the Fundamentals section of Coddy's Dart journey — lesson 22 of 94.

Comparison operators in Dart return boolean values, essential for decision-making.

void main() {
  int a = 10;
  int b = 10;
  bool areEqual = a == b;
  
  print('Is $a equal to $b? $areEqual');
}

Dart offers: equality (==), inequality (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

challenge icon

Challenge

Beginner

Create a program that compares two game scores using comparison operators:

  1. Declare two integer variables: playerScore with a value of 85 and highScore with a value of 90
  2. Use the comparison operators to check:
    • If playerScore equals highScore
    • If playerScore is not equal to highScore
    • If playerScore is greater than 80
    • If playerScore is less than highScore
    • If playerScore is greater than or equal to 85
    • If playerScore is less than or equal to 100
  3. Store each comparison result in a boolean variable with a descriptive name
  4. Print all the results with labels exactly as shown in the expected output

Your output must match this exact format:

Player score: 85
High score: 90
Scores are equal: false
Scores are different: true
Player score > 80: true
Player score < High score: true
Player score >= 85: true
Player score <= 100: true

Cheat sheet

Comparison operators in Dart return boolean values for decision-making:

  • == - equality
  • != - inequality
  • > - greater than
  • < - less than
  • >= - greater than or equal to
  • <= - less than or equal to
void main() {
  int a = 10;
  int b = 10;
  bool areEqual = a == b;
  
  print('Is $a equal to $b? $areEqual');
}

Try it yourself

void main() {
  // Declare the score variables here
  int playerScore = ?;
  int highScore = ?;
  
  // Perform comparisons and store results in boolean variables
  bool scoresEqual = playerScore ? highScore;
  bool scoresDifferent = playerScore ? highScore;
  bool scoreAbove80 = playerScore ? 80;
  bool scoreLessThanHigh = playerScore ? highScore;
  bool scoreAtLeast85 = playerScore ? 85;
  bool scoreAtMost100 = playerScore ? 100;
  
  // Print the scores and comparison results
  print("Player score: $playerScore");
  print("High score: $highScore");
  print("Scores are equal: $scoresEqual");
  print("Scores are different: $scoresDifferent");
  print("Player score > 80: $scoreAbove80");
  print("Player score < High score: $scoreLessThanHigh");
  print("Player score >= 85: $scoreAtLeast85");
  print("Player score <= 100: $scoreAtMost100");
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals