https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Christian Sousa

07/17/2020, 3:39 PM
Hello! How can one write something like this in Kotlin?
Copy code
let finished = false
for (let i = 0; i < restrictions.length && !finished; i++) {

      finished = ...

    }
d

Dominaezzz

07/17/2020, 3:40 PM
break
with
for (i in 0 until restrictions.length)
?
👆 1
d

diesieben07

07/17/2020, 3:41 PM
Depending on what
restrictions
is, you might be able to use
for (i in restrictions.indices)
or even just
for (element in restrictions)
c

Christian Sousa

07/17/2020, 3:41 PM
The only problem I have is the comparation with
!finished
. Should I just break inside the
for
?
👌 8
c

Casey Brooks

07/17/2020, 3:47 PM
val finished = restrictions.any { ... }
for a more ideomatic approach
s

Shawn

07/17/2020, 7:09 PM
@Casey Brooks I wouldn’t necessarily call that more idiomatic depending on what logic is actually in the for body — if there’s side effects or any particularly complex logic that determines when the loop is finished (or maybe even how
i
advances), then
.any {}
wouldn’t be the right tool for the job
c

Christian Sousa

07/20/2020, 4:24 PM
Thanks for you answers! It worked out just fine with the first answer, thanks @Dominaezzz
👍 1
2 Views