Updated March 2, 2023
Introduction of For Loop In Kotlin
For loop works with anything that provides an Iterator; in Kotlin, for loop is used to iterate through ranges, arrays, maps, and anything that provides an iterator. Furthermore, for loop is a control statement; it allows the code to be executed repeatedly until the final value is fulfilled; as soon as the final value is reached and fulfilled, the loop exits. To simplify the definition of For Loop, we can say that for loop is used when the range is known and when the User knows the exact range value.
Syntax of For Loop in Kotlin
The syntax of for loop is similar to other loops, but in for loop, it depends on the values given in the loop; as soon as the loop satisfies the value keyed in, the loop exits with the desired result if the condition is not met the loop will continue to execute.
The syntax of for loop is picturized below:-
Flowchart of For Loop
Below is the Flowchart of For loop; when the main program is executed, and the programs find a for loop, the loop starts executing within the given condition; if the condition is true, it continues executing until the final value is reached. If the condition is false, then the loop is terminated.
How For Loop Works in Kotlin?
In Kotlin, for loop is used to perform through ranges, arrays, etc.
For example, if you want to create a for loop to print numbers from 1 to 8, your program will be:-
fun main(args:Array<String>)
{
for (x in 1..8)
{
println(x)
}
}
In the above example, the for loop will run and return eight times as the range given is from 1 to 8, so it will print numbers in the range.
So when you run the program, the output will be:
- For loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly
- For loop are typically used when the number of iterations is known beforehand
- Once a value is initialized in the for loop, the loop works until the final value is filled, and then it prints the value.
- Kotlin for loop does the same thing as other languages for loop
- Kotlin only reduces the code with the same for loop functionality.
- Kotlin for loop allows you to rerun the same lines of code but is easier for the developers.
- Range in Kotlin is defined as .. so .. is the range in for loop.
- So 1..50 would mean a number from 1 to 50.
Examples of for loop in Kotlin
There are different ways to use for loop; a few are the below examples:-
Example #1
Code:
fun main(args:Array<String>)
{
print("loops prints (x in 4 downTo 1) print(x) = ")
for (x in 4 downTo 1) print(x)
}
In the above program, the loop will start running and will print the value from 4 to 1 as follows:
Example #2
Code:
fun main(args:Array<String>)
{
print("loop prints(a in 6 downTo 1 step 2) print(a) = ")
for (a in 6 downTo 1 step 2)
print(a)
}
In the above example, the loop will run from 6 to 1 range as the method here is downTo which runs from higher to lower value and will skip the value because of step 2 and will print 642
Let’s explore a few examples using an Array:
Example #3
Code:
fun main(args:Array<String>)
{
val newArray = arrayOf("fo", "r", "lo", "op")
for (string in newArray)
{
println(string)
}
}
For the above program, the output will be the string mentioned in the array and enclosed in “ ”
The output of the above program will be:-
Example #4
Code:
fun main(args: Array<String>)
{
var language = arrayOf("Loop1", "Loop2", "Loop3", "Loop4")
for (item in language)
println(item)
}
The above program will take the array and will print the output inserted in the loop as below:
Example #5
Let’s discuss an example of for loop using an array with an Index.
Code:
fun main(args: Array<String>) {
var language = arrayOf("Name1", "Name2", "Name3", "Name4")
for (cool in language.indices) {
// printing array elements having even index only
if (cool%2 == 0)
println(language[cool])
}
}
The above example works around an array and with an Index so that the above program will provide the below output:
Example #6
There is another way to use for loop is through a String; here is the example below:-
Code:
fun main(args: Array<String>)
{
var text= "Example"
for (letter in text)
{
println(letter)
}
}
In the above program, the loop will use the text inserted under the var variable and is initialized; then, the loop will print the text from the first letter to the end so that the text EXAMPLE will be printed as below:
Example #7
Here is another example of using for loop through a String with an Index.
Code:
fun main(args: Array<String>)
{
var text= "Examples"
for (item in text.indices)
{
println(text[item])
}
}
In the above example, the loop will print the same result as in the previous example; the only difference here is that it is using indices so that the output will be:-
Example #8
One more example of for loop is as below,
Code:
fun main(args:Array<String>)
{
var new = listOf(4, 8, 9, "New1", "is", "New2")
for(element in new)
{
println(element)
}
}
For the above program, a variable is initialized as new in which the input is given, so here the for loop will print the value present in the element, which is later initialized as new.
Take a look at the output of the code:
Conclusion
For loop in Kotlin is the same as in other Programming Languages; it performs the same functionality as in other programming languages, but in Kotlin, the loop code is simple, easy for the developers, and easy to understand.
For loops has two parts: a header that specifies the iteration and a body executed every time throughout the iteration.
The header always defines the range, and the body executes the iteration and the range defined; for loop is used when the iteration number is known before entering the loop.
Recommended Articles
This is a guide to For Loop in Kotlin. Here we discuss the introduction, syntax, and working of For Loop in Kotlin along with the flowchart and examples. You may also look at the following articles to learn more –