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:

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:

Comparison Operators with Booleans

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

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:

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!

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