0

Help for in or forEach Kotlin

What is the difference and which is best between fun main(args: Array<String>) { var arr = arrayOf(1, 3, 5) for (i in arr) { println(i*4) } } and fun main(args: Array<String>) { var arr = arrayOf(1, 3, 5) arr.forEach { println(it * 4) } }

23rd Jun 2021, 8:57 AM
Bi Cacti
Bi Cacti - avatar
2 ответов
0
In case if you want to traverse only the array. You can use both the loops(any one of it) But usign for each u can neither use conditions like print odd numbers, nor you can reverse the array
23rd Jun 2021, 10:41 AM
Atul [Inactive]
0
In case use for each loop because it reduces the chances of errors
23rd Jun 2021, 10:42 AM
Atul [Inactive]