Introduction
Control flow statements in Kotlin allow you to direct the order of execution of statements in your program. These statements are essential for making decisions, looping through code, and managing the flow of your application. This chapter will cover the different types of control flow statements available in Kotlin, along with examples and their syntax.
Types of Control Flow Statements
1. if
Statement
The if
statement is used to execute a block of code if a specified condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
fun main() {
val a = 10
if (a > 5) {
println("a is greater than 5")
}
}
2. if-else
Statement
The if-else
statement provides an alternative block of code to execute if the specified condition is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
fun main() {
val a = 10
if (a > 15) {
println("a is greater than 15")
} else {
println("a is not greater than 15")
}
}
3. if-else if-else
Statement
The if-else if-else
statement allows you to test multiple conditions.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
Example:
fun main() {
val a = 10
if (a > 15) {
println("a is greater than 15")
} else if (a > 5) {
println("a is greater than 5 but not greater than 15")
} else {
println("a is not greater than 5")
}
}
4. when
Statement
The when
statement is used to replace multiple if-else if
statements. It is similar to the switch
statement in other languages.
Syntax:
when (value) {
case1 -> {
// Code to execute if value == case1
}
case2 -> {
// Code to execute if value == case2
}
else -> {
// Code to execute if value does not match any case
}
}
Example:
fun main() {
val day = 3
when (day) {
1 -> println("Monday")
2 -> println("Tuesday")
3 -> println("Wednesday")
else -> println("Other day")
}
}
5. for
Loop
The for
loop is used to iterate over a range, array, or collection.
Syntax:
for (item in collection) {
// Code to execute for each item
}
Example:
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
for (number in numbers) {
println(number)
}
}
6. while
Loop
The while
loop is used to repeatedly execute a block of code as long as the specified condition is true.
Syntax:
while (condition) {
// Code to execute as long as condition is true
}
Example:
fun main() {
var i = 1
while (i <= 5) {
println(i)
i++
}
}
7. do-while
Loop
The do-while
loop is similar to the while
loop, but it guarantees that the block of code will be executed at least once.
Syntax:
do {
// Code to execute
} while (condition)
Example:
fun main() {
var i = 1
do {
println(i)
i++
} while (i <= 5)
}
8. break
Statement
The break
statement is used to terminate the loop or the nearest enclosing loop immediately.
Syntax:
break
Example:
fun main() {
for (i in 1..10) {
if (i == 5) {
break
}
println(i)
}
}
9. continue
Statement
The continue
statement is used to skip the current iteration of the loop and proceed to the next iteration.
Syntax:
continue
Example:
fun main() {
for (i in 1..10) {
if (i == 5) {
continue
}
println(i)
}
}
Example Program with Control Flow Statements
Here is an example program that demonstrates the use of various control flow statements in Kotlin:
fun main() {
val number = 7
// if-else statement
if (number % 2 == 0) {
println("$number is even")
} else {
println("$number is odd")
}
// when statement
when (number) {
1 -> println("Number is one")
2 -> println("Number is two")
3 -> println("Number is three")
else -> println("Number is not 1, 2, or 3")
}
// for loop
for (i in 1..5) {
println("For loop iteration: $i")
}
// while loop
var count = 1
while (count <= 5) {
println("While loop iteration: $count")
count++
}
// do-while loop
var count2 = 1
do {
println("Do-while loop iteration: $count2")
count2++
} while (count2 <= 5)
// break and continue in a loop
for (i in 1..10) {
if (i == 3) {
continue // Skip the rest of the loop when i is 3
}
if (i == 7) {
break // Terminate the loop when i is 7
}
println("Loop iteration with break/continue: $i")
}
}
Conclusion
In this chapter, you learned about the various control flow statements available in Kotlin, including if
, if-else
, if-else if-else
, when
, for
, while
, do-while
, break
, and continue
. Understanding these statements is crucial for controlling the flow of your program and making decisions based on conditions.