Kotlin For Loop: A Complete Guide for Beginners and Experts

Sudish Kumar
3 min readFeb 21, 2021

For loop is an important part of programming. In this tutorial we understand for loop how it works and how can we reduce codes.

This tutorial is divided into two parts, it’s first part of For loop in Kotlin .

Codes can be written in any language like, kotlin , Java, c , c++ … . But concept will be same or flow will be same .

I have learned for before 1years ago and on that time. I wasn’t understand it totally there was some confusion in this. Now I have used it a lot then I understand it. Now it’s more clear or I understand it.

Ex,

I have to print one to five without hardcore typed data and without any loop.

val a:Int = 1
val b:Int = 2
val a:Int = 3
val b:Int = 4
val e:Int = 5
For (a in 1..5) {
println(a)
println(b)
println(c)
println(d)
println(e)
}
Result,
12345

Let’ understood

What is for loop ?

For loop is a continuous process which i works on conditions.

It has a range (a initial point or starting point last point) , variable , in keyword and it’s body or lamda expression {}. You can see below.

It starts from starting point and end with a end point . And what ever you want to do you can do Or take action in body .

After using for loop codes are reduced.

Let’s see an example - 1

For (a in 1..5) {
print(a)
}

In this example aur staring point is 1 end point is 5 , variable is a ( variable name can be any thing ) and range is 1 to 5 it’s mean.

Result

12345

Our look will start form 1 and end when variable value will be 5.

Let’s dry run,

When for loop will be start then first time in a =1, because we have given it’s range 1 to 5 so staring point is 1 or first time variable value will be 1 .

And next time variable value will be 2 or a = 2 because it increase By default 1 .

And after that variable value will be 3 or a=3, then a=4 and in last a= 5 after that for loop will be stopped because it’s end point is 5.

Result,

12345

Difference between print() and println()

print() :- It only prints what ever is given in the print we can print any type of value.

println () :- It prints what ever is given in the println() and break the line we can print any type of value.

Ex-2

For (a in 1..5) {
println(a)
}
Result,
1
2
3
4
5

Flow will be same only there is line break because we are using println().

Ex -3

For (a in 1..5) {
println(a)
}
Result,
*****

It’s printing * because we are not printing value of variable only we are printing *.

When value of variable will be under the range what ever you want to do you can do in body or {} .

Like, storing value, printing value, adding conditions or using if else and more.

Practice makes us perfect so please do more practice you will learn more.

Thank you friends.

for(i in 1..5){
for(j in i..5){
print(" ")
}
for(k in 1..i){
print("* ")
}
println()
}
for(i in 4 downTo 1){
for(j in i..5){
print(" ")
}
for(k in 1..i){
print("* ")
}
println()
}

Thank you that’s all for this article if you want to see more example and pyramids on it please clap, follow and subscribe for update.

--

--

Sudish Kumar

Teach lead, Flutter Developer, Android Developer, Node Js Developer