2

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?

3 Answers 3

8

You can also use repeat() function:

repeat(10){
  //loop logic here
}
Sign up to request clarification or add additional context in comments.

Comments

5

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

(1..10).forEach {
    println("hello")
}

Comments

0

Kotlin also knows the convention of naming lambda parameters "_" in order to indicate they are unused. The following looks very similar to your example:

(1..10).forEach { _ ->
  //loop logic here
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.