Control Flow & when

Kotlin is a modern, concise language where decisions are made with if expressions and the powerful when construct — a supercharged replacement for the switch statement.

Learn Control Flow & when in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll use if as an expression (no ternary needed) and wield when with values, ranges, conditions, and type checks.

What You'll Learn in This Lesson

1️⃣ if as a Statement and an Expression

if / else chooses between paths just like in any language. The Kotlin twist is that if is also an expression — it evaluates to a value you can assign or return. That's why Kotlin has no separate ternary operator.

The line val label = if (...) "warm" else "cool" replaces the cond ? a : b you might know from other languages — clearer and using a keyword you already understand.

2️⃣ The when Expression

when is Kotlin's answer to the switch statement, but far more capable. It can match exact values, several values per branch, ranges with in , arbitrary boolean conditions, and types with is . Used as an expression it returns a value, and then an else branch is required to cover every case.

Notice the third block: after is String - the compiler smart-casts item to String , so item.length just works.

Your turn. Fill in the TODO , then run and compare.

Use a when expression to map a light colour to advice. Remember the else branch.

📋 Quick Reference — Control Flow

Practice quiz

Why does Kotlin have no ternary operator (a ? b : c)?

  • Because it lacks if/else
  • Because ternaries are banned
  • Because if is already an expression that returns a value
  • Because when replaces if

Answer: Because if is already an expression that returns a value. if (cond) a else b returns a value, so a ternary would be redundant.

What does val label = if (t > 20) "warm" else "cool" demonstrate?

  • if used as an expression returning a value
  • A syntax error
  • A loop
  • A type cast

Answer: if used as an expression returning a value. if is an expression, so its result can be assigned to a variable.

What is the arrow used in each when branch?

  • :
  • =>
  • ==
  • ->

Answer: ->. Kotlin when branches use condition -> result.

How do you match several values in one when branch?

  • Repeat the branch
  • Separate them with commas, like 2, 3, 4 ->
  • Use && between them
  • It is impossible

Answer: Separate them with commas, like 2, 3, 4 ->. List the values separated by commas: 2, 3, 4 -> ...

Which when branch checks a range?

  • score in 80..89 ->
  • score == 80..89 ->
  • score has 80..89 ->
  • range(80, 89) ->

Answer: score in 80..89 ->. Use the in operator with a range: score in 80..89 ->.

When is an else branch required in a when?

  • Never
  • Always
  • Only with strings
  • When when is used as an expression and the branches are not exhaustive

Answer: When when is used as an expression and the branches are not exhaustive. An expression must produce a value for every input, so a non-exhaustive when needs else.

Does a Kotlin when need break statements like a switch?

  • Yes, after every branch
  • No, only the matching branch runs, no break needed
  • Only for the else branch
  • Only inside loops

Answer: No, only the matching branch runs, no break needed. There is no fall-through; only the matched branch executes.

What happens after an is String -> branch in a when?

  • A ClassCastException
  • Nothing useful
  • The value smart-casts to String inside that branch
  • The branch is skipped

Answer: The value smart-casts to String inside that branch. A type branch smart-casts the value, so String members work directly.

Which keyword introduces a when branch that matches any remaining case?

  • else
  • default
  • otherwise
  • any

Answer: else. else is the catch-all branch in a when.

What does a when expression with no argument (when { ... }) let you write?

  • Full boolean conditions per branch, like an if/else-if chain
  • Only type checks
  • Only ranges
  • Nothing, it is invalid

Answer: Full boolean conditions per branch, like an if/else-if chain. Argument-free when evaluates a boolean condition in each branch.