HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to KotlinVariables (val / var)Data TypesStringsListsConditions (if / when)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Kotlin›Conditions (if / when)

Conditions (if / when)

Decision making with if-else, if returning a value, and multi-branched when structure.

Simple enough for a 5-year-old to understand

if / else

If a condition is true, the first block runs; otherwise, the else block runs.

Example code
1
val number = 7
2
if (number % 2 == 0) {
Is it evenly divisible by two?
ConditionIs it evenly divisible by two?
3
println("even")
If the condition is true.
Output / InputIf the condition is true.
4
} else {
5
println("odd")
If the condition is false.
Output / InputIf the condition is false.
6
}
ConditionOutput / Input
💡 Hint: Equality is checked with a double equals sign; a single equals sign means assignment.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which operator checks whether two values are equal?

About this topic

Conditions in Kotlin are written with if and when. Both have an important difference from other languages: they are not just control flow structures, but also expressions that produce values. You can assign the result of an if block directly to a variable.

That is why there is no ternary operator in Kotlin; the if expression fulfills that need. Similarly, when acts as a replacement for switch while being much more flexible: it can match against ranges, type checks, and conditions.

The condition expression must be a Boolean. You cannot use a number as a truth value; this prevents both accidental assignments and ambiguity in intent.

What you will learn

  • Using if and else
  • Using if as an expression
  • The when structure and its branch formats
  • The requirement for conditions to be Boolean
  • How smart cast works

Who is this for?

  • Learners of control flow
  • Those coming from Java and looking for a ternary operator
  • Those seeing the when structure for the first time

When to use?

if is preferred for two-way decisions, and when for multi-branch decisions; when is both more readable and the compiler can check for missing branches.

If you are branching based on the type of a value, doing a type check with when is both shorter and safer than writing consecutive if statements.

Common mistakes

Wrong

val count = 5
if (count) {
  println("full")
}

Kotlin requires the condition to be a Boolean and does not consider a number a truth value; you get the error "Type mismatch: inferred type is Int but Boolean was expected". You are expected to write the comparison explicitly.

Correct

val count = 5
if (count > 0) {
  println("full")
}

Wrong

val score = 85
val grade = if (score > 90) "A"

When if is used as an expression, the else branch is mandatory, because if the condition is not met, there will be no value left to assign to the variable. The compiler gives the error "'if' must have both main and 'else' branches if used as an expression".

Correct

val score = 85
val grade = if (score > 90) "A" else "B"

Wrong

val value: Any = "hello"
if (value is String) {
  println((value as String).length)
}

The explicit cast here is unnecessary: Kotlin accepts the value as that type after a type check, which is called a smart cast. Extra cast code creates clutter and the compiler warns you that it is redundant.

Correct

val value: Any = "hello"
if (value is String) {
  println(value.length)
}

Frequently asked questions

Why is there no ternary operator in Kotlin?

Because if is already an expression and does the same job more readably. When you want to produce a value based on a condition, you write if-else and assign the result; there is no need to learn a separate syntax.

Is else mandatory in when?

Yes, if you are using it as an expression that produces a value, because a result is needed in every case. If you are working with a finite type covering all possibilities, such as an enum, and you have written all cases, else is not required.

When does smart cast not work?

It does not work if the value can change in the meantime; for example, the compiler cannot guarantee it for a property defined with var and accessible from elsewhere. In such cases, assigning the value to a local val first solves the problem.

Summary

The condition expression must be a Boolean.

if is an expression, it produces a value.

When if is used as an expression, else is mandatory.

when is preferred for multi-branch decisions.

Smart cast comes into play after type casting.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code