ProgramGuru

When expressions and matching

When expressions and matching

A report needs grade messages for several ranges. Kotlin when compares a subject with branch conditions and can return the selected branch’s value. It provides a readable alternative to a long chain when the choices concern one subject.

Branches can match values, ranges or type checks. The first matching branch executes, and there is no automatic fallthrough. A value-producing when must cover the possibilities required by its type, often using else. A subjectless when can organize independent Boolean conditions.

What you’ll learn

  • Match the first appropriate when branch.
  • Distinguish closed and half-open ranges.

When expressions and matching in the example

  1. Read score

    A report needs grade messages for several ranges.

    Read score
  2. Match first eligible branch

    Branches can match values, ranges or type checks.

    Match first eligible branch
  3. Return message

    Eighty-two matches the second range and assigns Pass.

    Return message
Eighty-two matches the second range and assigns Pass. The half-open range excludes 90, while the first closed range includes it. No later branch executes after the match.

Follow when expressions and matching

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

fun main() {
  val score = 82
  val result = when (score) {
    in 90..100 -> "Excellent"
    in 75..<90 -> "Pass"
    else -> "Review score"
  }
  println(result)
}

Expected output

Pass

Follow the execution

Eighty-two matches the second range and assigns Pass. The half-open range excludes 90, while the first closed range includes it. No later branch executes after the match.

Try this in the Kotlin compiler by pasting the complete program.

Practice: When expressions and matching

Your task: Which branch handles score 90?

Kotlin/JVM · complete program

fun main() {
  val score = 82
  val result = when (score) {
    in 90..100 -> "Excellent"
    in 75..<90 -> "Pass"
    else -> "Review score"
  }
  println(result)
}

Worked answer

The first branch handles 90 because 90..100 includes that endpoint.

When expressions and matching: the important boundary

A default branch can also catch impossible values such as 120. Validate the domain separately instead of calling every unmatched score an ordinary low grade.

Check your understanding

Which branch handles score 90?
Choose one answer. You can try again.

Answer and explanation

The first branch handles 90 because 90..100 includes that endpoint..

The first branch handles 90 because 90..100 includes that endpoint.

When expressions and matching: what to keep in mind

The first branch handles 90 because 90..100 includes that endpoint. A default branch can also catch impossible values such as 120.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: For loops and ranges →