🔁 Kotlin Control Flow Statements – Conditions, Loops, Ranges & More
🧲 Introduction – Direct Program Logic with If, When, Loops & Breaks
Control flow statements determine how your Kotlin program makes decisions and repeats actions. These include conditional expressions (if, when), loops (for, while, do-while), and branching statements like break and continue.
Kotlin improves control structures with expression-based syntax, eliminating boilerplate and offering greater flexibility with concise logic.
🎯 In this guide, you’ll learn:
- How to use if...elseandwhenexpressions for decision making
- How to iterate using forandwhileloops
- How to control flow inside loops using breakandcontinue
- How to leverage Kotlin’s rangesfor loop iterations and validations
📘 Topics Covered
| 🔍 Topic | 📖 Description | 
|---|---|
| 🧭 Kotlin – Control Flow Overview | Summary of Kotlin’s conditional and loop constructs. | 
| 🔀 Kotlin – If…Else Expressions | Conditional branching using if,else, and nested expressions. | 
| 🧩 Kotlin – When Expression | Kotlin’s flexible replacement for switch-case logic. | 
| 🔁 Kotlin – For Loop | Iterating over arrays, ranges, and collections using for. | 
| 🔄 Kotlin – While Loop | Repeating tasks using whileanddo...whileloops. | 
| 🧷 Kotlin – Break & Continue | Skipping or exiting loops with break,continue, and labeled flow. | 
| 📏 Kotlin – Ranges | Creating iterable numeric ranges using ..,until, andstep. | 
🔍 Detailed Sections & Examples
🔀 Kotlin – If…Else Expressions
val score = 85
val grade = if (score >= 90) "A"
            else if (score >= 75) "B"
            else "C"
println(grade)  // Output: B
🧠 Explanation:
- ifis an expression that returns a value.
- Supports multi-line, nested, and inline expressions.
🧩 Kotlin – When Expression (Switch Alternative)
val day = 3
val result = when(day) {
    1 -> "Monday"
    2 -> "Tuesday"
    3 -> "Wednesday"
    else -> "Unknown"
}
println(result)  // Output: Wednesday
💡 when can replace complex if-else chains with clean branching.
🔁 Kotlin – For Loop
val items = listOf("Kotlin", "Java", "Swift")
for (item in items) {
    println(item)
}
📌 Iterate through any iterable (lists, arrays, ranges) using for (element in collection).
🔄 Kotlin – While Loop
var i = 0
while (i < 3) {
    println("i = $i")
    i++
}
🌀 Use while when the loop count isn’t known beforehand.
🔁 Kotlin – Do…While Loop
var num = 0
do {
    println("Running at least once: $num")
    num++
} while (num < 1)
⏩ do...while ensures the block runs at least once.
🧷 Kotlin – Break & Continue
for (i in 1..5) {
    if (i == 3) continue
    if (i == 5) break
    println(i)
}
📌 Output:124
- continueskips current iteration.
- breakexits the loop.
📏 Kotlin – Ranges
for (i in 1..3) {
    println(i)
}
🧠 Kotlin offers:
- 1..5➝ inclusive range
- 1 until 5➝ excludes 5
- 5 downTo 1➝ reverse range
- 1..10 step 2➝ custom steps
📌 Summary – Recap & Next Steps
Kotlin’s control flow is concise and powerful, giving developers a functional approach to conditional logic and iteration. Its expression-based if and when, combined with intuitive loops and range syntax, streamline everyday logic.
🔍 Key Takeaways:
- Use if...elseandwhenas expressions that return values.
- Iterate collections or ranges with for,while, anddo...while.
- Control loops with break,continue, and custom ranges.
⚙️ Practical Use Cases:
- Menu handling using when
- Iterating API data with loops
- Conditional formatting and filters in logic-heavy code
❓ Frequently Asked Questions
❓ Can if be used as an expression in Kotlin?
✅ Yes. if returns a value and can be assigned to a variable:
val result = if (score > 50) "Pass" else "Fail"
❓ What makes when more powerful than switch in Java?
✅ Kotlin’s when supports ranges, type checks, expressions, and has no fallthrough:
when (x) {
    in 1..10 -> println("In range")
    else -> println("Out of range")
}
❓ How do I break out of nested loops in Kotlin?
✅ Use labeled breaks:
outer@ for (i in 1..3) {
    for (j in 1..3) {
        if (i == 2) break@outer
        println("$i, $j")
    }
}
❓ What is the difference between 1..5 and 1 until 5?
✅ 1..5 includes 5, 1 until 5 excludes 5.
❓ Can I use step with downTo?
✅ Yes. Example:
for (i in 10 downTo 1 step 2) {
    println(i)
}
Share Now :
