Kotlin supports various types of loops that allow developers to execute a group of statements multiple times.
For loop
The For loop in Kotlin loops through all elements of a collection. The formal syntax of For loop in Kotlin is as follows.
Variable is the name of the variable that applies to each element in the Sequence is used to iterate over. Within the loop body, all statements that should be executed for each element in the sequence can be executed.
The following example calculates squares of all numbers from 1 to 9 using a for loop:
And it works like this:
for(n in 1..9): This is where the for loop begins, which includes the range 1/9/XNUMX up to and including XNUMX/XNUMX/XNUMX goes through.
println(“${n * n}”): Each time the loop is run (iteration of the loop), the current value of n squared (with n*n) and using the function print () output in console.
It is possible to nest loops within each other.
In summary, this program performs the following steps:
- The outer loop loops through the numbers of 1 – 9 (including) for the variable i.
- For any value of i the inner loop is started and runs through the numbers of 1 – 9 (including 9) for the variable j.
- For every couple i and j Your product is calculated and displayed in the table.
- After the inner loop for all values of j has been traversed, a new row is added and the output moves to the next row of the multiplication table.
- The program ends after all rows of the multiplication table for the numbers of 1 – 9 were issued.
There are also other types of For loops, which can be used in Kotlin:
For loop with an index: In this type of for loop, an index is used to iterate over a collection of elements and get the index of each element at the same time.
For loop with a step size: In this type of for loop, a step size can be specified to control the number of loop iterations.
And another way to use the step size in combination with the index:
For loop with one condition: In this type of for loop, a condition can be specified to control which elements are processed in the loop.
while loop
In Kotlin the while loop used to execute a task repeatedly as long as a certain condition is met. The loop ends as soon as the condition is no longer met. Here are some examples of using while loops in Kotlin:
In this example, a while loop used to represent the numbers of 1 – 10 to spend. The loop will execute as long as the value of count Smaller or equal limit at a hunt.
Do..while loop
This is in Kotlin do..while loop a control structure that repeatedly executes a block of code as long as a certain condition is met. In contrast to the while loop, the code block inside the do..while loop executed at least once, regardless of the condition. The condition is only checked after the first run of the code block.
Here is the syntax of the do..while loop:
Here is an example showing how to use a do..while loop in Kotlin:
This example uses a do..while loop to get the numbers from 1 – 5 to spend. The loop will execute as long as the value of count Smaller or equal limit is. Since the code block within the do..while loop is executed at least once, the number 1 always printed, even if the value of limit is less than 1 is. Try this yourself by setting the value of the variable limit to 0 puts.
Control statements continue and break
In Kotlin are keep on going and break Control statements inside loops like for, while or do..while be used to control the flow of the loop.
The continue statement is used to indicate the current iteration of the loop to end early and continue directly with the next run. If keep on going is executed, the rest of the code within the loop block for the current iteration is skipped and the loop continues with the next iteration. Example of using continue:
In this example the continue statement used to skip all odd numbers (i % 2 != 0) and only even numbers in between 1 and 10 to spend.
The break statement is used to the to end the entire loop early. If break is executed, the loop is aborted immediately and the program flow continues after the loop block. Example of using break:
In this example, a for loop used to represent the numbers of 1 – 10 to spend. However, the loop is interrupted by the break statement aborted as soon as the value of i equal 5 is. This means that the numbers 1 – 4 are output and the loop then ends.