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
BeginnerCreate a program that compares two game scores using comparison operators:
- Declare two integer variables:
playerScorewith a value of 85 andhighScorewith a value of 90 - Use the comparison operators to check:
- If
playerScoreequalshighScore - If
playerScoreis not equal tohighScore - If
playerScoreis greater than 80 - If
playerScoreis less thanhighScore - If
playerScoreis greater than or equal to 85 - If
playerScoreis less than or equal to 100 - Store each comparison result in a boolean variable with a descriptive name
- 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");
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number