Kotlin – Control Flow Overview: Guide to Making Decisions in Code
Introduction – Why Learn Kotlin Control Flow?
Control flow statements are the core decision-making tools in Kotlin. They let your programs respond to different conditions, repeat actions, and handle multiple branches. Kotlin streamlines control flow using powerful constructs like if, when, for, while, and more—making your code clean, safe, and expressive.
In this guide, you’ll learn:
- The purpose and types of control flow statements
- How to make decisions using
if,else, andwhen - How to repeat tasks using loops like
for,while, anddo-while - Best practices and real-world use cases
Types of Control Flow Statements in Kotlin
| Category | Constructs | Description |
|---|---|---|
| Conditional | if, if-else, when | Make decisions based on conditions |
| Looping | for, while, do-while | Repeat code while a condition is true |
| Control Jump | break, continue, return | Exit or skip parts of loops/functions |
1. Conditional Statements
if and if-else:
val age = 20
if (age >= 18) {
println("Adult")
} else {
println("Minor")
}
Kotlin also supports if as an expression:
val status = if (age >= 18) "Adult" else "Minor"
when Expression – Kotlin’s Smart Switch:
val score = 85
val grade = when (score) {
in 90..100 -> "A"
in 80..89 -> "B"
in 70..79 -> "C"
else -> "Fail"
}
println("Grade: $grade")
when replaces long if-else-if chains and can be used as a statement or an expression.
2. Looping Statements
for Loop:
for (i in 1..5) {
println("Count: $i")
}
Supports ranges, arrays, collections, and indices.
while Loop:
var x = 3
while (x > 0) {
println(x)
x--
}
do-while Loop:
var y = 1
do {
println("Running at least once")
y--
} while (y > 0)
Executes the block at least once, even if the condition is false.
3. Jump Statements
break:
Exits the nearest loop:
for (i in 1..10) {
if (i == 5) break
println(i)
}
⏭️ continue:
Skips the current iteration:
for (i in 1..5) {
if (i == 3) continue
println(i)
}
return:
Exits from a function early:
fun check(num: Int) {
if (num < 0) return
println("Positive number: $num")
}
Smart Usage – Combining Control Flow
val input = readLine()
val response = when {
input.isNullOrBlank() -> "No input"
input.length > 5 -> "Long input"
else -> "Valid input"
}
println(response)
Summary – Recap & Next Steps
Control flow is the foundation of logic in every Kotlin program. It helps your code respond dynamically to input, data, or user actions. Kotlin makes control flow concise, expressive, and powerful using familiar yet improved constructs.
Key Takeaways:
- Use
if,whenfor conditional branching - Repeat actions with
for,while, anddo-while - Manage loop execution using
break,continue, andreturn - Combine expressions and control structures for cleaner code
Practical Use:
Control flow powers user authentication, validation, loops in games, conditional API calls, and much more in real-world Kotlin applications.
FAQs – Kotlin Control Flow
What is the difference between if and when in Kotlin?
if is used for simple true/false conditions. when is more powerful for handling multiple conditions and ranges.
Can if return a value in Kotlin?
Yes. Kotlin allows if to be used as an expression:
val max = if (a > b) a else b
What’s the difference between while and do-while?
while checks the condition first. do-while runs the block at least once before checking.
How does break work in Kotlin loops?
break exits the nearest enclosing loop immediately.
Can I use return to exit early from a function?
Yes. return exits the current function or lambda and skips remaining code.
Share Now :
