Introduction
Comparison operators help us check if values are equal or which one is bigger or smaller. This is useful to make decisions in programs.
Comparison operators help us check if values are equal or which one is bigger or smaller. This is useful to make decisions in programs.
a == b // equals a != b // not equals a > b // greater than a < b // less than a >= b // greater than or equal to a <= b // less than or equal to
Use double equals (==) to check if two values are the same.
Use != to check if two values are different.
val x = 5 val y = 10 println(x == y) // false
val a = 7 val b = 7 println(a != b) // false
val age = 18 println(age >= 18) // true
val score = 75 println(score < 50) // false
This program checks if the temperature is hot, cold, or exactly 30 degrees using comparison operators.
fun main() { val temperature = 30 val isHot = temperature > 25 val isCold = temperature < 15 println("Is it hot? $isHot") println("Is it cold? $isCold") println("Is temperature exactly 30? ${temperature == 30}") }
Comparison operators return a Boolean value: true or false.
Use parentheses {} inside strings to show the result of comparisons.
Comparison operators help compare two values.
They return true or false depending on the comparison.
Common operators: ==, !=, >, <, >=, <=.
== is used to check if two values are equal.== returns true if both values are equal, otherwise false.a is not equal to variable b?!= to check if two values are not equal.a != b is correct; a <> b is invalid in Kotlin; a =! b is incorrect syntax; a !== b checks referential inequality, not value inequality.val x = 10 val y = 20 println(x > y)
x > y compares if 10 is greater than 20, which is false.false.val a = 5
val b = 10
if (a = b) {
println("Equal")
} else {
println("Not Equal")
}a = b, which is assignment, not comparison.== to compare values, so it should be a == b.val a = 7 val b = 7 val c = 10 println((a == b) && (b < c))
a and b are 7, a == b is true.b < c is true.true && true is true.Pass the test and this pattern moves unknown