18
Kotlin Tutorial - 3 Control Flow (Iteration)
In Kotlin, there are two main types to create an iteration or loop by using while
and for
.
In while
loop, the condition is checked first before the code inside while
block is executed. This is the basic syntax of while
loop.
while(condition) {
// code..
}
In this example, the while
loop is used to prints out the number from 1
until 4
.
fun main() {
var num = 1
while(num <= 4) {
println(num)
num++
}
}
Output
1
2
3
4
Based on the code above, the while
loop has a flow like this:
- Check the condition.
- If the condition is true, execute the codes inside
while
block. - If the condition is false, stop the execution.
Notice that inside while
block, there is a code that used to increase the num
value by 1 with ++
operator. The num++
equals to num = num + 1
.
Here it is another similiar operator that usually used together with loop mechanism.
Operator | Description |
---|---|
+= |
a += b equals to a = a + b
|
-= |
a -= b equals to a = a - b
|
*= |
a *= b equals to a = a * b
|
/= |
a /= b equals to a = a / b
|
%= |
a %= b equals to a = a % b
|
++ |
a++ equals to a = a + 1
|
-- |
a-- equals to a = a - 1
|
There is another type of while
loop called do while
loop. The do while
loop executes the code inside do while
block then check the condition. This is the basic syntax of do while
loop.
do {
// code..
} while(condition)
In this example, the do while
loop is used.
fun main() {
// create variable called num
var num = 1
// use do while loop
do {
// prints out the num's value
println(num)
// increase the num's value by 1
num++
} while (num < 0)
}
Output
1
Based on the code above, the code inside do while
block is executed first then the condition of num
value is checked.
For loop is a loop mechanism that suitable for range condition. This is the basic syntax of using for
loop.
for(range_condition) {
// code..
}
In this example, the for
loop is used to prints out number from 1 until 4.
fun main() {
for (num in 1..4) {
println(num)
}
}
Output
1
2
3
4
Based on the code above, the range condition is specified using in
keyword followed with specified range.
Another way to create range condition is using until
keyword.
var_name in initial_value until limit_value
This is the example of using until
keyword.
fun main() {
for(num in 1 until 3) {
println(num)
}
}
Output
1
2
Based on the code above, the num
value that printed out is 1
and 2
because the 3
acts as a limit value only. If the num
value equals with the limit value (in this case is 3
), the code execution inside for
block is stopped.
The for
loop is suitable for iterating through collection like List
. In this example, the for
loop is used to iterate through list
.
fun main() {
// create a list of integers
val list = listOf(1,2,3,4)
// using for loop
// to iterate through every list items
for(item in list) {
println(item)
}
}
Output
1
2
3
4
Based on the code above, the specified range is item in list
which means every item exists in list
, execute the code inside for
block. The code inside for
block is executed to prints out the value from item
temporary variable.
The nested for
loop is also available. In this example, the nested for
loop is used to iterate through list of collections.
fun main() {
// create a list of collections
val database = listOf(
listOf("Docker","Git","Ansible"),
listOf("Kotlin","Java","Golang")
)
// using nested for loop
for (list in database) {
for (item in list) {
println(item)
}
}
}
Output
Docker
Git
Ansible
Kotlin
Java
Golang
Based on the code above, the inner loop is executed first then the outer loop is executed.
The break
and continue
keyword usually used together with loop mechanism. The continue
keyword means continue the execution of code whilst break
keyword means stop the code execution.
In this example, the continue
keyword is used to print out only odd values.
fun main() {
for (num in 1..10) {
if (num % 2 == 0) {
continue
}
println(num)
}
}
Output
1
3
5
7
9
Based on the code above, the continue
keyword is used to execute into the next loop which means if num % 2 == 0
condition is true, the num
value is skipped then the next loop is executed.
In this example, the break
keyword is used to stop the execution inside while
loop.
fun main() {
var num = 1
while(true) {
if (num == 3) break
println(num)
num++
}
}
Output
1
2
Based on the code above, the break
keyword is used to stop the execution if the value from num
variable is equals 3
.
- While loop is suitable for unexact condition.
- For loop is suitable for range or exact condition.
- Learn more about iteration or loop in Kotlin by visiting this link.
I hope this article is helpful for learning the Kotlin programming language. If you have any thoughts or comments you can write in the discussion section below.
18