Kotlin While Loop

Kotlin While Loop – Complete Beginner Guide With Examples
Loops are one of the most important concepts in programming. They allow you to repeat a block of code multiple times without writing it again and again.
In Kotlin, the while loop is used when you want to execute code as long as a condition remains true.
In this SEO-optimized, beginner-friendly guide, you will learn:
What a while loop is
How it works in Kotlin
Basic syntax
While vs do-while
Infinite loops
Using break and continue
Real-world examples
Common beginner mistakes
Best practices
Let’s begin
What Is a While Loop in Kotlin?
A while loop repeatedly executes a block of code as long as a given condition evaluates to true.
Once the condition becomes false, the loop stops.
Basic Syntax of While Loop
Here’s how it works:
Kotlin checks the condition.
If true → code runs.
Condition is checked again.
If false → loop stops.
Simple Example of While Loop
Output:
Explanation:
Loop runs while
numberis less than or equal to 5.number++increases value each time.
How While Loop Works Step-by-Step
Let’s analyze:
Execution:
count = 3 → true → prints 3
count = 2 → true → prints 2
count = 1 → true → prints 1
count = 0 → false → loop stops
While Loop vs For Loop
| Feature | While Loop | For Loop |
|---|---|---|
| Best for | Unknown iterations | Fixed range |
| Condition check | Before loop body | Before loop body |
| Readability | Simple | Structured |
| Index control | Manual | Automatic |
Use while loop when:
Number of iterations is unknown.
Loop depends on dynamic condition.
Real-World Example – Password Validation
This continues until correct password is entered.
Using break in While Loop
The break statement stops the loop immediately.
Example:
Output:
The loop exits when i equals 5.
Using continue in While Loop
The continue statement skips the current iteration.
Example:
Output:
Iteration with value 3 is skipped.
Infinite While Loop
An infinite loop runs forever if condition never becomes false.
Example:
This loop never stops unless:
break is used
Program is terminated
Controlled Infinite Loop Example
This is a practical use case.
What Is Do-While Loop?
The do-while loop runs at least once.
Syntax:
Example of Do-While
Output:
Difference:
while → checks first
do-while → runs once, then checks
While Loop with Boolean Condition
This loop depends on a boolean variable.
Nested While Loop
You can use while loops inside another while loop.
Example:
This creates a grid-like output.
Common Beginner Mistakes
Forgetting to Update Variable
This creates infinite loop.
Always update loop variable.
Wrong Condition
Incorrect condition may prevent loop from running.
Using While Instead of For
Use while only when needed.
Performance Considerations
While loops are efficient when:
Used properly
Condition is simple
Avoid unnecessary calculations inside loop
Avoid:
Complex conditions
Heavy operations inside loop
Practical Example – Sum of Numbers
Output:
Practical Example – Countdown Timer
When Should You Use While Loop?
Use while loop when:
You don’t know how many iterations are needed
Loop depends on user input
Loop depends on condition change
Building interactive systems
Avoid while loop when:
Fixed range is known (use for loop instead)
Frequently Asked Questions (FAQs)
1. What is a while loop in Kotlin?
A while loop repeats a block of code as long as a condition is true.
2. What is the difference between while and do-while?
While checks condition before running. Do-while runs once before checking condition.
3. Can while loop run forever?
Yes, if condition never becomes false.
4. When should I use while loop?
Use it when number of iterations is unknown.
5. Can I use break inside while?
Yes, break stops the loop immediately.
Conclusion
The Kotlin while loop is a powerful control flow structure used to execute repeated tasks based on a condition.
You learned:
Basic syntax
While vs do-while
Using break and continue
Infinite loops
Real-world examples
Common mistakes
Mastering while loops will strengthen your understanding of Kotlin control flow.
