Chapter 6: Control Flow: Conditions

Control Flow – Conditions — this is the chapter where Kotlin really starts to feel like magic compared to Java!

Kotlin’s control flow is cleaner, more expressive, and much more powerful — especially the when expression (which is like a super-charged switch on steroids) and if as an expression.

We’re going to go super slowly, like we’re sitting together in a quiet Bandra café with cutting chai ☕ — I’ll explain every concept with real-life analogies, lots of copy-paste examples, step-by-step breakdowns, tables, common mistakes, and fun facts so everything sticks perfectly.

Let’s dive in!

1. if as Expression – Kotlin’s First Big Win

In Java → if is just a statement (does something, no return value). In Kotlin → if is an expression → it returns a value!

Real-life analogy: In Java → “If it’s raining, take umbrella” → just an instruction. In Kotlin → “If it’s raining, take umbrella, otherwise take sunglasses” → you get something back (umbrella or sunglasses).

Basic if statement (same as Java)

Kotlin

if as expression → returns the last expression of the block

Kotlin

Even shorter (single-line branches)

Kotlin

Very common pattern: assign result directly

Kotlin

2. when – Super-Powered Switch (Kotlin’s Star Feature)

when is much more powerful than Java’s switch. It’s an expression (returns value), supports types, ranges, collections, etc.

Basic when as statement

Kotlin

when as expression (returns value – most common!)

Kotlin

Powerful features of when

  1. Type checking (smart cast!)
Kotlin
  1. Ranges (super useful!)
Kotlin
  1. Collection check with in
Kotlin
  1. Without argument (like if-else chain)
Kotlin

3. Ranges in Kotlin – Very Powerful

Kotlin has beautiful range syntax — used everywhere (in when, for, if, etc.)

Syntax Meaning Includes end? Example
.. Closed range (includes both ends) Yes 1..5 → 1,2,3,4,5
until Open range (excludes end) No 1 until 5 → 1,2,3,4
downTo Decreasing range Yes 5 downTo 1 → 5,4,3,2,1
step Jump by step (1..10).step(2) → 1,3,5,7,9

Examples

Kotlin

4. in Operator with Collections

in works beautifully with lists, sets, maps, strings, ranges…

Kotlin

5. Quick Recap Table (Your Cheat Sheet)

Feature Kotlin Way (Best Practice) Java Equivalent (for comparison)
if as expression val max = if (a > b) a else b int max = (a > b) ? a : b;
when as statement when (day) { 1 -> … } switch (day) { case 1: … }
when as expression val type = when (day) { … } No direct equivalent
Range (inclusive) 1..10 No direct equivalent
Range (exclusive) 1 until 10 No direct equivalent
in operator x in list, x in 1..10 list.contains(x)

6. Common Newbie Mistakes & Fixes

Mistake Problem Fix
Using == instead of in for ranges if (marks == 80..100) → wrong! Use marks in 80..100
Forgetting else in when expression Compile error (when must be exhaustive) Add else -> …
Using switch keyword Doesn’t exist in Kotlin Use when instead
Writing if without else in expression Compile error (must return value) Always provide else branch
Confusing .. and until Off-by-one errors Remember: .. includes end, until excludes

7. Homework for You (Let’s Make It Fun!)

  1. Basic Ask user for a number (using readln()) → use when to print if it’s positive, negative, or zero.
  2. Medium Ask user for marks (0–100) → use when with ranges to print grade (A+, A, B, C, Fail).
  3. Advanced Write a function fun getDayMessage(day: Int) that returns message using when (Monday → “Start week!”, Weekend → “Relax!”).
  4. Fun Use in to check if a character is vowel → ask user for a letter → print “Vowel” or “Consonant”.
  5. Challenge Fix this buggy code:
    Kotlin

You’ve just unlocked Kotlin’s most loved control flow — when and expression-style if!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *