Skip to content

Commit ef1b809

Browse files
authored
Merge pull request mouredev#5830 from eulogioep/main
#17 - Kotlin
2 parents e6badaa + 89654a1 commit ef1b809

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
fun main() {
2+
println("Diferentes mecanismos de iteración en Kotlin:")
3+
4+
// 1. Usando un bucle for con un rango
5+
println("\n1. Usando for con rango:")
6+
for (i in 1..10) {
7+
print("$i ")
8+
}
9+
println()
10+
11+
// 2. Usando un bucle while
12+
println("\n2. Usando while:")
13+
var j = 1
14+
while (j <= 10) {
15+
print("$j ")
16+
j++
17+
}
18+
println()
19+
20+
// 3. Usando forEach con una lista
21+
println("\n3. Usando forEach con lista:")
22+
(1..10).toList().forEach { print("$it ") }
23+
println()
24+
25+
// 4. Usando repeat
26+
println("\n4. Usando repeat:")
27+
repeat(10) { print("${it + 1} ") }
28+
println()
29+
30+
// 5. Usando do-while
31+
println("\n5. Usando do-while:")
32+
var k = 1
33+
do {
34+
print("$k ")
35+
k++
36+
} while (k <= 10)
37+
println()
38+
39+
// 6. Usando un iterador
40+
println("\n6. Usando un iterador:")
41+
val iterator = (1..10).iterator()
42+
while (iterator.hasNext()) {
43+
print("${iterator.next()} ")
44+
}
45+
println()
46+
47+
// 7. Usando secuencias
48+
println("\n7. Usando secuencias:")
49+
(1..10).asSequence().forEach { print("$it ") }
50+
println()
51+
52+
// 8. Usando un bucle for con índices
53+
println("\n8. Usando for con índices:")
54+
for (index in 0 until 10) {
55+
print("${index + 1} ")
56+
}
57+
println()
58+
59+
// 9. Usando takeWhile
60+
println("\n9. Usando takeWhile:")
61+
generateSequence(1) { it + 1 }
62+
.takeWhile { it <= 10 }
63+
.forEach { print("$it ") }
64+
println()
65+
66+
// 10. Usando fold
67+
println("\n10. Usando fold:")
68+
(1..10).fold("") { acc, i -> "$acc$i " }.also { print(it) }
69+
println()
70+
}

0 commit comments

Comments
 (0)