Kotlin, an expressive and concise programming language, offers various control flow mechanisms to handle logic in your code. In this post, we'll explore if
expressions, when
expressions, and loops, which are fundamental for controlling the flow of execution based on certain conditions. Let’s dig into each of these constructs!
if
ExpressionIn Kotlin, if
is not just a statement; it can also be used as an expression. This means that you can use it to return values. Below is a basic example to illustrate:
fun main() { val number = 10 val message = if (number > 0) { "The number is positive." } else if (number < 0) { "The number is negative." } else { "The number is zero." } println(message) }
In the code above:
number
and set it to 10
.if
expression to check whether the number is positive, negative, or zero.message
variable.This behavior shows the versatility of the if
expression in Kotlin.
when
ExpressionThe when
expression in Kotlin is a powerful and elegant way to handle multiple conditions. It can be seen as a more readable alternative to a series of if
statements. Here's how you can use it:
fun main() { val day = 2 val dayName = when (day) { 1 -> "Monday" 2 -> "Tuesday" 3 -> "Wednesday" 4 -> "Thursday" 5 -> "Friday" 6 -> "Saturday" 7 -> "Sunday" else -> "Invalid day" } println(dayName) }
In this example:
day
and assign it the integer 2
.when
expression, we check the value of day
against various options.The when
expression also has an interesting feature where you can check ranges or conditions:
fun main() { val score = 85 val grade = when { score >= 90 -> "A" score >= 80 -> "B" score >= 70 -> "C" score >= 60 -> "D" else -> "F" } println("Your grade is $grade") }
In this snippet, instead of using specific values, we are checking ranges of scores to assign grades.
Kotlin offers several types of loops: for
, while
, and do while
. Let’s break down each type.
The for
loop is used for iterating over ranges, arrays, or collections:
fun main() { for (i in 1..5) { println("Number $i") } }
In the code above:
1
to 5
using the ..
operator, which creates a range.You can also loop over arrays or collections:
fun main() { val fruits = arrayOf("Apple", "Banana", "Cherry") for (fruit in fruits) { println(fruit) } }
The while
loop continues executing as long as a specified condition is true:
fun main() { var count = 1 while (count <= 5) { println("Count is $count") count++ } }
Here, the loop continues to execute until count
exceeds 5
, incrementing with each iteration.
The do while
loop executes at least once before checking the condition:
fun main() { var count = 1 do { println("Counting down: $count") count++ } while (count <= 5) }
In this example, even if the initial condition is false, the loop will execute at least one time.
By understanding and utilizing these control flow constructs like if
, when
, and loops, you can create powerful and flexible Kotlin applications. In your journey with Kotlin, remember that these basics will form the foundation of more complex logic in your code. Happy coding!
21/09/2024 | Kotlin
21/09/2024 | Kotlin
03/09/2024 | Kotlin
21/09/2024 | Kotlin
21/09/2024 | Kotlin
21/09/2024 | Kotlin
03/09/2024 | Kotlin
21/09/2024 | Kotlin