Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn kotlin While Loops


Sure! The while loop in Kotlin is used to repeatedly execute a block of code as long as a condition is true. Here's a simple explanation with an example:

Syntax

Example

while (condition) {
// Code to be executed repeatedly
}

Example

Let's say we want to print numbers from 1 to 5 using a while loop:

Example

fun main() {
var count = 1 // Initialize a variable

while (count <= 5) { // Condition: Repeat until count is less than or equal to 5
println("Count: $count") // Print the value of count
count++ // Increment count by 1
}
}

Output

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Explanation

Conclusion

The while loop is a fundamental control flow construct in Kotlin that allows you to repeat a block of code based on a condition. It's useful for scenarios where you need to iterate over a sequence of values or perform actions until a certain condition is met.



Advertisement





it pathshaala : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java