Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
How to add a condition to a for loop in Kotlin?

I know that in Java, it is possible to do the following:

boolean condition = true;

for(int i=0; i<array.length && condition; i++){

}

If the condition is false, the for loop stops, but, how to do the same in Kotlin?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

there is no such option in Kotlin (availabe constructions in HERE), but you can add additional check and break keyword

for (i in 0..array.length) {
    if (!condition) break; // quit loop without further iteration
    //rest of code
over 4 years ago · Santiago Trujillo Denunciar

0

You can also use the below approach for the conditional for loop.

(0..array.length).takeWhile {
    condition
}.forEach {
    // do something with `it (index)`
}
over 4 years ago · Santiago Trujillo Denunciar

0

fun checkCondition(){
  val condition=true
  outerloop@for(i in array.indices) {
    if(array[i]!=condition){
      condition=false
      break@outerloop
    }
  }
  return condition
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda