Kotlin Logical, Assignment and Increment Operators

Kotlin course · Operators · Lesson 12 of 51

You already know the arithmetic and comparison operators. This lesson covers the rest of the everyday operators: logical operators to combine conditions, assignment shortcuts, increment/decrement, and the handy in operator.

Logical operators: &&, ||, !

OperatorNameTrue when
&&andboth sides are true
||orat least one side is true
!notthe value is false
fun main() {
    val age = 20
    val hasTicket = true

    println(age >= 18 && hasTicket)   // both true
    println(age < 12 || hasTicket)    // one is true
    println(!hasTicket)               // flips true to false
}

Output:

true
true
false

Truth table

aba && ba || b
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

Short-circuit evaluation

Kotlin stops checking as soon as it knows the answer. With &&, if the left side is false, the right side never runs. With ||, if the left side is true, the right side never runs. This lets you guard against errors:

fun main() {
    val name: String? = null
    // name.length would crash, but it's never reached
    println(name != null && name.length > 3)

    val list = listOf<Int>()
    println(list.isEmpty() || list[0] > 10)  // list[0] is never evaluated
}

Output:

false
true

Assignment operators

OperatorSame as
a += ba = a + b
a -= ba = a - b
a *= ba = a * b
a /= ba = a / b
a %= ba = a % b
fun main() {
    var score = 10
    score += 5
    println(score)
    score *= 2
    println(score)
    score -= 6
    println(score)
    score %= 7
    println(score)
}

Output:

15
30
24
3

These only work on var. A val can’t be changed.

Increment and decrement: ++ and —

fun main() {
    var count = 5
    count++
    println(count)
    count--
    println(count)

    var a = 5
    println(a++)   // postfix: prints 5, then a becomes 6
    println(a)
    var b = 5
    println(++b)   // prefix: b becomes 6, then prints 6
}

Output:

6
5
5
6
6

When ++ is on its own line, prefix and postfix do the same thing. The difference only matters inside a larger expression. To keep code clear, use ++ as a separate statement.

The in operator

in checks whether a value is inside a range or a collection. !in checks that it isn’t:

fun main() {
    val marks = 72
    println(marks in 60..100)      // range check
    println(marks !in 0..32)

    val vowels = listOf('a', 'e', 'i', 'o', 'u')
    println('e' in vowels)
    println("Kot" in "Kotlin")     // substring check
}

Output:

true
true
true
true

marks in 60..100 is easier to read than marks >= 60 && marks <= 100.

Equality: == and ===

OperatorChecks
==the values are equal
===both are the same object in memory

You’ll almost always use ==. === is mainly useful with objects you create yourself.

Operator precedence

When you mix operators, Kotlin evaluates them in this order (highest first):

  1. ++, --, ! (on one value)
  2. *, /, %
  3. +, -
  4. .. (ranges)
  5. in, !in
  6. <, >, <=, >=
  7. ==, !=
  8. &&
  9. ||
  10. =, +=, -= and the other assignments

So a || b && c means a || (b && c). When in doubt, add brackets: they cost nothing and make the code clearer.

Common mistakes

  • Using and/or words: Kotlin’s and and or exist but don’t short-circuit. Use && and || for conditions.
  • Using = instead of ==: if (a = 5) doesn’t compile in Kotlin, which saves you from a classic bug.
  • Modifying a val: val x = 1; x += 1 doesn’t compile. Use var.

Exercise

A shop gives free delivery when the order is ₹500 or more, or the customer is a member. Members also get 10% off orders between ₹1,000 and ₹5,000. Write the checks.

fun main() {
    val orderTotal = 1200
    val isMember = true

    val freeDelivery = orderTotal >= 500 || isMember
    val memberDiscount = isMember && orderTotal in 1000..5000

    var payable = orderTotal.toDouble()
    if (memberDiscount) payable *= 0.9

    println("Free delivery: $freeDelivery")
    println("Discount applied: $memberDiscount")
    println("Pay: ₹$payable")
}

Output:

Free delivery: true
Discount applied: true
Pay: ₹1080.0

FAQ

What is the difference between && and & in Kotlin?

Kotlin has no & operator for Booleans. It uses the word and, which works like && but always evaluates both sides. Use && in conditions.

Does Kotlin have a ternary operator?

No. if is an expression in Kotlin, so you write val x = if (a > b) a else b. You’ll learn this in the next lesson.

What does in do in Kotlin?

It checks whether a value is in a range, collection or string, for example x in 1..10.

WhatsApp us📞 Call now