Kotlin While Loop

Kotlin Tutorial

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:

  1. Kotlin checks the condition.

  2. If true → code runs.

  3. Condition is checked again.

  4. If false → loop stops.


Simple Example of While Loop


 

Output:

1
2
3
4
5

Explanation:

  • Loop runs while number is 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

FeatureWhile LoopFor Loop
Best forUnknown iterationsFixed range
Condition checkBefore loop bodyBefore loop body
ReadabilitySimpleStructured
Index controlManualAutomatic

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:

1
2
3
4

The loop exits when i equals 5.


Using continue in While Loop

The continue statement skips the current iteration.

Example:


 

Output:

1
2
4
5

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:

1
2
3

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:

Sum: 15

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.

You may also like...