-
-
Save parzibyte/e924e650b10bf8d7ee7701a5c6c1dc4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(argumentos: Array<String>) { | |
// Rangos en Kotlin | |
// By parzibyte | |
// https://parzibyte.me/blog | |
val rangoDeNumeros = 1..5 | |
for (numero in rangoDeNumeros) { | |
println(numero) | |
} | |
val rangoDeLetras = 'a'..'l' | |
for (letra in rangoDeLetras) { | |
println("Rango de letras de menor a mayor: $letra") | |
} | |
val rangoDeLetrasMayusculas = 'A'..'L' | |
for (letra in rangoDeLetrasMayusculas) { | |
println("Tengo una letra mayúscula: $letra") | |
} | |
if ('F' in rangoDeLetrasMayusculas) { | |
println("La letra F existe") | |
} | |
val rangoDeCaracteres = '@'..'z' | |
for (letra in rangoDeCaracteres) { | |
println("Tengo un caracter: $letra") | |
} | |
// No se pueden crear rangos de mayor a menor, pero | |
// sí se puede invertir uno | |
val rangoDeLetrasInvertido = ('a'..'z').reversed() | |
for (letra in rangoDeLetrasInvertido) { | |
println("Rango de letras de mayor a menor: $letra") | |
} | |
// Podemos avanzar por pasos, y sí, usar negativos | |
val otroRangoDeNumeros = (-10..100).step(2) | |
for (numero in otroRangoDeNumeros) { | |
println("Número en pasos de 2: $numero") | |
} | |
// Los pasos también funcionan con otros rangos | |
val otroRangoDeLetras = ('o'..'r').step(3) | |
for (letra in otroRangoDeLetras) { | |
println("Letra en pasos de 3: $letra") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment