Control Flow Statements in Kotlin

Welcome to the tutorial on control flow statements in Kotlin! Control flow statements allow you to control the execution flow of your program based on certain conditions or iterate over a set of elements. In this tutorial, we will explore the if statement, when statement, for loop, and while loop in Kotlin.

The if Statement

The if statement is used to make decisions based on a condition. It can be used in two forms: if and if-else. Here's an example:


val number = 10

if (number > 0) {
    println("Number is positive")
} else {
    println("Number is non-positive")
}
  

In the example above, the if statement checks if the `number` is greater than 0. If the condition is true, it prints "Number is positive". Otherwise, it prints "Number is non-positive".

The when Statement

The when statement is used to perform different actions based on multiple conditions. It is similar to the switch statement in other programming languages. Here's an example:


val dayOfWeek = 3

when (dayOfWeek) {
    1 -> println("Monday")
    2 -> println("Tuesday")
    3 -> println("Wednesday")
    4 -> println("Thursday")
    5 -> println("Friday")
    else -> println("Weekend")
}
  

In the example above, the when statement checks the value of `dayOfWeek` and prints the corresponding day of the week. If none of the conditions match, it prints "Weekend".

Looping Statements: for and while

The for loop is used to iterate over a range, an array, or any other iterable object. Here's an example:


val numbers = arrayOf(1, 2, 3, 4, 5)

for (number in numbers) {
    println(number)
}
  

In the example above, the for loop iterates over the `numbers` array and prints each element.

The while loop is used to repeat a block of code as long as a condition is true. Here's an example:


var count = 0

while (count < 5) {
    println("Count: $count")
    count++
}
  

In the example above, the while loop prints the value of `count` and increments it until `count` reaches 5.

Common Mistakes with Control Flow Statements

  • Missing or incorrect conditions in if or when statements.
  • Forgetting to update the loop variable in a loop, leading to an infinite loop.
  • Using the wrong loop construct for the intended purpose, such as using a while loop when a for loop is more appropriate.

Frequently Asked Questions (FAQs)

  1. Q: Can I use multiple conditions in an if statement?

    A: Yes, you can use logical operators (such as `&&` for AND and `||` for OR) to combine multiple conditions in an if statement.

  2. Q: Can I have nested if statements?

    A: Yes, you can nest if statements inside other if statements to create more complex decision-making logic.

  3. Q: Can I use the when statement without an argument?

    A: Yes, you can use the when statement without an argument and specify conditions directly after the `->` symbol.

  4. Q: Can I use the break statement to exit a loop?

    A: Yes, you can use the `break` keyword to exit a loop prematurely if a certain condition is met.

  5. Q: Can I use the continue statement to skip an iteration in a loop?

    A: Yes, you can use the `continue` keyword to skip the current iteration and move to the next iteration in a loop.

Summary

Congratulations! You have learned about control flow statements in Kotlin. You explored the if statement, when statement, for loop, and while loop, and understood how they can be used to control the flow of your program. Remember to avoid common mistakes and choose the appropriate control flow construct for your needs. Keep practicing and applying these concepts in your Kotlin programs. Happy coding!