Syntax

Kotlin When Expression Syntax

In Kotlin, when expression: matches a value against cases — when (n) { 1 -> "one" else -> "other" }

Quick Answer

Syntax
when (n) { 1 -> "one" else -> "other" }
KotlinSyntax

When Expression

Code example
when (n) { 1 -> "one" else -> "other" }

Matches a value against cases.

Learn More

How to when expression in Kotlin

  1. Open your Kotlin file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Kotlin When Expression Syntax

Use when (n) { 1 -> "one" else -> "other" }.

Related Syntax References

Frequently asked questions

What is the Kotlin syntax for when expression?

Use this pattern: when (n) { 1 -> "one" else -> "other" }

Can I copy this Kotlin code example?

Yes. Use Copy code and adapt the example to your program.

Is this Kotlin syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify