Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

control-flow-kotlin

Control flow

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Control flow: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Coroutines replace callback hell on Android and in Ktor—structured concurrency is interview-critical.

You will apply Control flow in contexts like: Network calls, database I/O, and UI state on Android and Ktor servers.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Kotlin control flow resembles Java but many constructs are expressions that return values—if and when can assign directly to variables.

if and when

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

when (grade) {
    "A" -> println("Excellent")
    "B", "C" -> println("Pass")
    else -> println("Review")
}

Loops

for (item in collection), while, and do-while match familiar patterns. Ranges: 1..10, until, step.

Important interview questions and answers

  1. Q: when vs switch?
    A: when is expression-based, supports ranges, types, and no fall-through bugs like C-style switch.
  2. Q: Is if an expression?
    A: Yes—branches must be expressions or blocks with a single value when used in assignment.

Self-check

  1. How do you write an inclusive range 1 through 5?
  2. Can when replace an if-else chain?

Tip: when replaces switch without fall-through bugs—like modern C# switch expressions.

Interview prep

when vs switch?

when is expression-based without fall-through.

if expression?

Branches can return values for assignment.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • when vs switch?
  • Ranges for loop?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump