Menu
Coddy logo textTech

Comparison Operators

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 22 of 93.

Comparison operators let you compare two values and get a Boolean result, either true or false. These operators are essential for making decisions in your programs.

Here are Kotlin's comparison operators:

OperatorMeaningExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal5 >= 5true
<=Less than or equal3 <= 5true

You can use these with variables and store the results:

fun main() {
    val age = 18
    val isAdult = age >= 18
    println(isAdult)  // Prints: true
    
    val score = 85
    val isPerfect = score == 100
    println(isPerfect)  // Prints: false
}

Comparison operators work with any numeric types and are the foundation for decision-making logic you'll learn in upcoming lessons.

challenge icon

Challenge

Easy

Write a function checkScores that takes score1, score2, and passingScore and returns a string with three comparison results.

Compare the two scores against each other and against the passing threshold using comparison operators.

Logic:

  1. Check if score1 is greater than score2
  2. Check if score1 equals score2
  3. Check if score2 is greater than or equal to passingScore

Parameters:

  • score1 (Int): The first score
  • score2 (Int): The second score
  • passingScore (Int): The minimum passing score

Returns: A string with three boolean results separated by spaces. Format: [result1] [result2] [result3]

Example: For inputs 85, 70, and 60, return "true false true" because 85 > 70 is true, 85 == 70 is false, and 70 >= 60 is true.

Try it yourself

fun checkScores(score1: Int, score2: Int, passingScore: Int): String {
    // Write code here
}
quiz iconTest yourself

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

All lessons in Fundamentals

Practice on your own: Kotlin playground