Syntax

Kotlin If Expression Syntax

In Kotlin, if expression: if returns a value — val max = if (a > b) a else b

Quick Answer

Syntax
val max = if (a > b) a else b
KotlinSyntax

If Expression

Code example
val max = if (a > b) a else b

If returns a value.

Learn More

How to if 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 If Expression Syntax

Use val max = if (a > b) a else b.

Related Syntax References

Frequently asked questions

What is the Kotlin syntax for if expression?

Use this pattern: val max = if (a > b) a else b

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