Dart Comparison Operators: Making Decisive Code Comparisons

Lesson Introduction and Plan

Greetings! Today, we're diving into the exciting realm of Dart comparison operators. These operators play a significant role in directing the execution of code based on comparisons.

Our core objective is to understand comparison operators and their use in a Dart programming context. We will delve into various Dart comparison operators and enrich your understanding through practical, real-world examples.

Dart Comparison Operators Overview

Imagine monitoring a sports championship. Outcomes depend on evaluating conditions such as the scores of the competing teams. These decisions allude back to comparisons, which resemble situations in programming. In Dart, we utilize comparison operators to make such decisions.

Dart has six comparison operators: equivalent to (==), not equivalent to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). They yield true or false results, known as boolean values.

Consider the following: a comparison of one team's score to that of another:

Dart
int teamAScore = 150;  // score of Team A
int teamBScore = 130;  // score of Team B
print('Is Team A leading Team B?');
print(teamAScore > teamBScore);
// Prints: Is Team A leading Team B? true

In the Dart code snippet above, we compared teamAScore and teamBScore using the > operator. The output is true because teamAScore is indeed higher than teamBScore.

Venturing Into == and != Operators

Let's further explore the equivalent to (==) and not equivalent to (!=) operators. These are necessary when you need to compare values, such as comparing the current team's fouls to the allowable ones:

Dart
int currentFouls = 5;  // current number of fouls
int permittedFouls = 5;  // allowable number of fouls

bool haveReachedLimit = currentFouls == permittedFouls;  // the outcome is 'true'
bool isUnderLimit = currentFouls != permittedFouls;  // the outcome is 'false'

The == operator checks whether currentFouls equals permittedFouls, yielding a true outcome. On the other hand, the != operator checks for their inequality, resulting in false.

Venturing Into <, >, <=, and >= Operators

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal