Lesson 4
Understanding Kotlin Comparison Operators: A Beginner's Guide
Introduction and Lesson Overview

Greetings! Today, we're studying comparison operators in Kotlin, which are vital for decision-making in coding. Through engaging examples with different data types, we'll grasp the power of these operators. Let's begin our exploration!

Understanding Comparison Operators

Imagine a daily competitive event where we judge who is taller or faster. Replace these with "greater," "lesser," "equal," or "not equal," and you've got comparison operators! Kotlin uses six basic ones:

  1. Equal to (==)
  2. Not equal to (!=)
  3. Greater than (>)
  4. Less than (<)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

Let's explore these through simple examples:

Kotlin
1val num1 = 5 2val num2 = 10 3 4println(num1 == num2) //prints: false, as 5 is not equal to 10 5println(num1 != num2) //prints: true, as 5 is not equal to 10 6println(num1 > num2) //prints: false, as 5 is not greater than 10 7println(num1 < num2) //prints: true, as 5 is less than 10 8println(num1 >= num2) //prints: false, as 5 is not greater than or equal to 10 9println(num1 <= num2) //prints: true, as 5 is less than or equal to 10

Comparing 5 and 10 using these operators produces boolean values (true or false).

Applying Comparison Operators on Numbers

Consider two brothers: Harry, who is 12, and Ron, who is 14. We can use age comparison operators to compare their ages:

Kotlin
1val ageHarry = 12 2val ageRon = 14 3 4println(ageHarry == ageRon) // prints: false, because Harry and Ron are of different ages 5println(ageHarry != ageRon) // prints: true, as above 6println(ageHarry > ageRon) // prints: false, because Harry is not older than Ron 7println(ageHarry < ageRon) // prints: true, because Harry is younger than Ron 8println(ageHarry >= ageRon) // prints: false, as Harry is not older or as old as Ron 9println(ageHarry <= ageRon) // prints: true, as Harry is younger or as old as Ron (in this case, younger)
Comparison Operators with Booleans

Now, let's compare Boolean variables. Note that == and != are the operators we use here:

Kotlin
1val isMorning = true 2val isAfternoon = false 3 4println(isMorning == isAfternoon) // prints: false, as true does not equal false 5println(isMorning != isAfternoon) // prints: true, as true is not equal to false
Comparing Characters and Strings

In Kotlin, when comparing characters (Char), the character that comes first alphabetically is considered "less than" the one that follows. However, it's important to note that capital letters are considered "less than" lowercase letters because of their order in the Unicode system. For strings (String), the comparison is made letter by letter from the beginning, determining which string comes first based on alphabetical order, taking into account the capitalization. Thus, a string with a capital letter might come before a similar string with its first letter in lowercase, just like organizing words in a dictionary where case affects placement. Let's compare the characters 'a' and 'b', and the strings "alpha" and "beta" using comparison operators:

Kotlin
1val char1 = 'a' 2val char2 = 'b' 3 4println(char1 == char2) // prints: false, because 'a' is not the same as 'b' 5println(char1 != char2) // prints: true, as 'a' and 'b' are different 6println(char1 > char2) // prints: false, as 'a' comes before 'b' in the alphabet 7println(char1 < char2) // prints: true, as above 8println(char1 >= char2) // prints: false, as 'a' is not after or in the same position as 'b' 9println(char1 <= char2) // prints: true, as 'a' is before or in the same position as 'b' (in this case, before) 10 11val string1 = "alpha" 12val string2 = "beta" 13 14println(string1 == string2) //prints: false, because "alpha" and "beta" are different words 15println(string1 != string2) //prints: true, as above 16println(string1 > string2) //prints: false, because "a" comes before "b" in the dictionary 17println(string1 < string2) //prints: true, because "a" comes before "b" in the dictionary 18println(string1 >= string2) //prints: false, because "a" comes before "b" in the dictionary 19println(string1 <= string2) //prints: true, because "a" comes before "b" in the dictionary

Remember, string comparisons are case-sensitive!

Lesson Summary and Upcoming Practice

Great work! Today, you learned how to apply Kotlin comparison operators to basic data types using a variety of examples. Stay tuned for hands-on exercises to solidify your knowledge. Let's continue exploring this exciting world of coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.