26
loading...
This website collects cookies to deliver better user experience
while
and for
.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..
}
while
loop is used to prints out the number from 1
until 4
.fun main() {
var num = 1
while(num <= 4) {
println(num)
num++
}
}
1
2
3
4
while
loop has a flow like this:while
block.while
block, there is a code that used to increase the num
value by 1 with ++
operator. The num++
equals to num = num + 1
.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
|
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)
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)
}
1
do while
block is executed first then the condition of num
value is checked.for
loop.for(range_condition) {
// code..
}
for
loop is used to prints out number from 1 until 4.fun main() {
for (num in 1..4) {
println(num)
}
}
1
2
3
4
in
keyword followed with specified range.until
keyword.var_name in initial_value until limit_value
until
keyword.fun main() {
for(num in 1 until 3) {
println(num)
}
}
1
2
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.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)
}
}
1
2
3
4
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.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)
}
}
}
Docker
Git
Ansible
Kotlin
Java
Golang
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.continue
keyword is used to print out only odd values.fun main() {
for (num in 1..10) {
if (num % 2 == 0) {
continue
}
println(num)
}
}
1
3
5
7
9
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.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++
}
}
1
2
break
keyword is used to stop the execution if the value from num
variable is equals 3
.