Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 6, 2019 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/fd4fd99532ddd3fb65138116989779f0 to your computer and use it in GitHub Desktop.
Save parzibyte/fd4fd99532ddd3fb65138116989779f0 to your computer and use it in GitHub Desktop.
fun main(argumentos: Array<String>) {
/**
* Ciclos en Kotlin:
* while
* do while
* for
*
* @author parzibyte
* @link https://parzibyte.me/blog
* */
// Ciclo del 0 al 9
// usando un rango
// https://parzibyte.me/blog/2019/07/05/rangos-kotlin/
val rangoDeNumeros = 0..9
for (numero in rangoDeNumeros) {
println(numero)
}
// Recorrer un arreglo
// Más sobre arreglos: https://parzibyte.me/blog/2019/07/02/arreglos-kotlin/
val nombres = arrayOf("John Galt", "Dagny Taggart", "Hank Rearden")
for (nombre in nombres) {
println(nombre)
}
// Ciclo con while y contadores
var contador = 5
while (contador > 0) {
println("Cuenta regresiva: $contador")
contador--
}
do {
println("Me ejecuto una vez")
} while (false)
// También existe el do while
var otroContador = 0
do {
println("Contador: $otroContador")
otroContador++
} while (otroContador < 5)
// Recorrer un arreglo:
var indice = 0
while (indice < nombres.size) {
println("Un nombre: ${nombres[indice]}")
indice++
}
// Ciclo con flotantes
var contadorFlotantes = 0.1
while (contadorFlotantes < 1.0) {
println("Contando del 0.1 al 1.0: $contadorFlotantes")
contadorFlotantes += 0.1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment