Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore loop constant in for loop

Tags:

kotlin

In Swift you can ignore the loop constant by using _ like so:

for _ in 0...10 {
  //loop logic here
}

Is there an equivalent in Kotlin?

like image 562
Dick Lucas Avatar asked Aug 19 '26 05:08

Dick Lucas


2 Answers

You can also use repeat() function:

repeat(10){
  //loop logic here
}
like image 152
Pawel Avatar answered Aug 22 '26 04:08

Pawel


You can just use a forEach loop, and don't use it inside the closure:

(1..10).forEach {
    println("hello")
}
like image 34
JB Nizet Avatar answered Aug 22 '26 02:08

JB Nizet