In this tutorial, we will explore how to handle control flow in Kotlin. Control flow refers to the order in which the program's code executes. We will delve into the syntax and usage of various control flow structures in Kotlin, such as conditional statements and loops.
By the end of this tutorial, you'll be able to:
- Understand and use conditional statements (if
, when
)
- Understand and use loops (for
, while
, do-while
)
- Use break
and continue
statements
To get the most out of this tutorial, you should be comfortable with basic programming concepts and have a basic understanding of Kotlin syntax.
In Kotlin, conditional statements are used to make decisions based on different conditions. The two main conditional statements in Kotlin are if
and when
.
The if
statement is the most basic control flow statement. It decides which statement to execute based on a Boolean condition.
Syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
The when
statement in Kotlin is like the switch
statement in other languages. It selects one of many code blocks to be executed.
Syntax:
when (variable) {
case1 -> // code to be executed if variable matches case1
case2 -> // code to be executed if variable matches case2
else -> // code to be executed if none of the cases match
}
Loops are used to execute a block of code repeatedly. Kotlin provides several loops: for
, while
, and do-while
.
The for
loop iterates through anything that provides an iterator. Here's the basic syntax:
for (item in collection) {
// code to be executed
}
The while
loop executes a block of code repeatedly as long as the condition is true.
Syntax:
while (condition) {
// code to be executed
}
The do-while
loop is similar to while
loop except that the condition is evaluated after the execution of the block of code. Therefore, a do-while
loop executes the block of code at least once.
Syntax:
do {
// code to be executed
} while (condition)
val number = 7
if (number > 10) {
println("Number is greater than 10")
} else {
println("Number is not greater than 10") // This will be printed
}
val score = 85
when {
score >= 90 -> println("Excellent")
score >= 80 -> println("Good") // This will be printed
else -> println("Not bad")
}
for (i in 1..5) {
println(i) // This will print numbers 1 to 5
}
var i = 1
while (i <= 5) {
println(i) // This will print numbers 1 to 5
i++
}
var i = 1
do {
println(i) // This will print numbers 1 to 5
i++
} while (i <= 5)
In this tutorial, we learned about control flow in Kotlin. We covered conditional statements (if
, when
) and loops (for
, while
, do-while
). To master these concepts, practice is key. Try to incorporate these structures in your Kotlin projects.
To further your understanding, you can check out Kotlin's official documentation on control flow: Kotlin Control Flow
Write a Kotlin program that prints the factorial of a number.
Write a Kotlin program that checks if a number is prime or not.
Write a Kotlin program that prints the Fibonacci series up to a given number.
Here's the solution to the first exercise:
fun factorial(n: Int): Long {
var result = 1L
for (i in 2..n) {
result *= i
}
return result
}
fun main() {
val num = 5
println("Factorial of $num = ${factorial(num)}") // Factorial of 5 = 120
}
In this code, we define a function that calculates the factorial of a number. We then call this function with a number to get the factorial.