Updated April 11, 2023
Introduction to Kotlin Loops
Like any programming language, in kotlin also, the loops concept is used. Loops are mainly used to repeat the specific task. For example, if we want to print the number from 1 to 10, instead of writing a print statement 10 times, we can implement it through loops. Loops reduce the number of lines in the program and improve reliability. Loops are control flow structures that controls the flow of the program within loops. there are three types of loops in kotlin, for loop, while loop, and do-while loop. In his article, we are going o discussed these loops in detail.
Types of loops in kotlin
There are three types of loops in kotlin – for loop, while loop, and do-while loop. Let’s discuss them one by one.
1. For loop in kotlin
Kotlin for loop is used to iterate the program several times. It iterates through ranges, arrays, collections, or anything that provides for iterate. The syntax of kotlin for loops is as follows
Syntax:
for(Item in collection)
{
body of the loop
}
for the keyword is used to implement for loop in kotlin. item is the name of the variable which is used to store the data or information. in is a keyword that is used to check the items in the collection. collection can be anything numbers, arrays, list, etc. for loops first check the condition, if the condition matches, it will execute the loop else will stop the loop and execute the other statements.
Code:
fun main(args: Array<String>) {
val num = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
println("The Numbers in the list are as follows:")
for(item in num)
{
println(item)
}
}
Output:
Explanation:
We have initialized the array from 1 to 10 using the num variable. For loop checks the condition and prints the numbers one by one. Here item is used as a variable to store the number from the array and print it one by one.
2. While loop in kotlin
Kotlin while loop is used to iterate the program serval times. It executes the block of the code until the result of the condition is true. The syntax of the kotlin while loop is as follows
Syntax:
while(condition)
{
body of the loop
}
while keyword is used to implement the while loop in kotlin. condition is used to validate the requirement of the program, based on the outcome, the condition is mentioned. While loop first checks the condition, if the condition is true, it will execute the block of the while. It repeat the same untill the condition is true. once the condition becomes false, it will transfer the flow of the program outside the loop.
Code:
fun main(args: Array<String>) {
println("Program to print number from 1 to 10")
var num = 1
while(num <= 10)
{
println(num)
num++
}
}
Output:
Explanation:
Here we have written a program to print the number of 1 to 10. First, we have initialized the num variable to 1. As we have to print the number from 1 to 10, we have mentioned the condition num <= 10. Inside the loop, the print is used to print the num and increment the num. First, it will Print the 1. num++ wi in cream the num by 1, again it checks the condition and prints 2. The same procedure is performed until the condition becomes false.
3. Do while loop in kotlin
Kotlin do-while loop is the same as while loop, only one difference is there. while loop first checks the condition and then executes the block of the code, whereas do-while loop, first execute the block of the code then checks the condition. The syntax of the do-while loop in kotlin is as follows
Syntax:
do
{
body of the loop
}
while(condition);
do keyword is used to implement the do-while loop in kotlin. do first execute the block of the code and then check the condition. If the condition is true, it will repeat the execution of the loop else stop the execution of the loop. while keyword is used to specify the condition which is ended by using; semicolon.
Code:
fun main(args: Array<String>) {
println("Program to print number from 1 to 15")
var num = 1
do
{
println(num)
num++
}
while(num <= 15);
}
Output:
Explanation:
Here we have written a program to print the numbers from 1 to 15 using a do-while loop in kotlin. We have initialized the num by 1. Then will execute the loop and print the 1 and increment the value by 1. Outside the loop, the while statement checks the condition. here we have mention num <= 15 as we have to print the numbers till 15. if the condition is true it will again execute the do loop else stop the program. Here you can also mention condition such as num < 16
Conclusion
Loop is a specific programming structure that repeats the sequence of some task until the condition matches. Here we have discussed the various kotlin loops such as for loop, while loop, and do-while loop with their syntax, working, and examples. Hope you enjoyed the article.
Recommended Articles
This is a guide to Kotlin Loops. Here we discuss the introduction and types of loops in kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –