Code for "for loop" in Kotlin?


Warning: Trying to access array offset on false in /home3/indiakep/public_html/wp-content/plugins/dw-question-answer/inc/Template.php on line 8
All QuestionsCategory: KotlinCode for "for loop" in Kotlin?
chetan shidling asked 5 years ago

I need short information.

1 Answers
chetan shidling answered 5 years ago

For loop in Kotlin.
 
fun main(args : Array<String>){
var nums = 1..10

for(a in nums)
{
println(a)
}
}
 
Output:
 
1
2
3
4
5
6
7
8
9
10
 
We can also increment in two steps or more steps, let’s see one example.
 
fun main(args : Array<String>){
var nums = 1..10

for(a in nums step 2)
{
println(a)
}
}
 
Output:
 
1
3
5
7
9