🔄 Kotlin – While Loop: Repeating Logic Until a Condition Fails
🧲 Introduction – Why Learn Kotlin While Loops?
The while loop in Kotlin is used when you need to execute a block of code repeatedly based on a dynamic condition. Unlike the for loop (used with ranges or collections), while is ideal when you don’t know the exact number of iterations in advance—such as reading input or waiting for an event.
🎯 In this guide, you’ll learn:
- The syntax and structure of whileanddo-whileloops
- Real-world use cases for both loop types
- Differences between whileandfor
- Best practices and common mistakes
🔁 Basic While Loop Syntax
var i = 1
while (i <= 5) {
    println("i = $i")
    i++
}
🟢 Output:
i = 1  
i = 2  
i = 3  
i = 4  
i = 5
- Executes the block as long as the condition is true.
- The condition is checked before each iteration.
🔂 Kotlin Do…While Loop
Use do…while when you want the block to run at least once, even if the condition is false.
var j = 1
do {
    println("j = $j")
    j++
} while (j <= 3)
🟢 Output:
j = 1  
j = 2  
j = 3
- Checks the condition after running the block.
⚖️ While vs Do…While
| Feature | while | do…while | 
|---|---|---|
| Condition check | Before loop starts | After loop runs once | 
| Guaranteed execution | No (may not run) | Yes (runs at least once) | 
| Use case | When unsure if loop should run | When block must run at least once | 
🧪 Practical Example – User Input (Simulated)
var input: String?
do {
    println("Enter something (type 'exit' to quit):")
    input = readLine()
} while (input != "exit")
🧠 This is a real-world use case of do…while, as the prompt should show at least once.
🔁 Infinite Loop with Break Condition
var x = 1
while (true) {
    println("x = $x")
    if (x == 3) break
    x++
}
✅ Use break to exit the loop manually when a condition is met.
⛔ Common Mistakes in While Loops
| ❌ Mistake | ✅ Solution | 
|---|---|
| Forgetting to update condition | Ensure loop variable changes inside loop | 
| Infinite loop unintentionally | Use break conditions or counter checks | 
| Off-by-one errors | Double-check conditions ( <=vs<) | 
| Skipping doindo-while | Remember, it’s do { ... } while (cond) | 
✅ Best Practices for While Loops
| Practice | Reason | 
|---|---|
| Use whilewhen condition is dynamic | Gives better control and clarity | 
| Add breakconditions for safety | Prevents infinite loops | 
| Use comments for complex loop logic | Improves readability and maintainability | 
| Use do-whilewhen guaranteed first execution | Reduces redundant checks | 
📌 Summary – Recap & Next Steps
Kotlin’s while and do-while loops are ideal for dynamic looping scenarios where the iteration count isn’t predetermined. They offer flexible control flow for input processing, polling systems, retry logic, and more.
🔍 Key Takeaways:
- Use whilefor conditional pre-check loops.
- Use do-whilewhen at least one execution is needed.
- Break infinite loops safely using break.
- Always ensure your condition changes within the loop.
⚙️ Practical Use:
While loops are often used in user interaction, file reading, data polling, or game loops—anywhere continuous checking is required.
❓ FAQs – Kotlin While Loops
❓ What’s the difference between while and do…while in Kotlin?
✅ while checks the condition before the first iteration; do…while runs the loop once before checking the condition.
❓ Can I write an infinite while loop in Kotlin?
✅ Yes. Use while (true) and break manually:
while (true) {
    if (someCondition) break
}
❓ Is do…while mandatory for repeated input prompts?
✅ Not mandatory, but preferred when you want the prompt to show at least once.
❓ Can I use continue inside a while loop?
✅ Yes. It skips the current iteration and moves to the next condition check:
while (i < 10) {
    i++
    if (i % 2 == 0) continue
    println(i)
}
❓ Can Kotlin while loops return values?
✅ No. Unlike expressions like if or when, while is a statement, not an expression—it doesn’t return a value.
Share Now :
