Conditional Statement in Kotlin

Topics Covered

Overview

Conditional statements in Kotlin, like in many programming languages, enable decision-making in code. The if statement in kotlin allows for straightforward conditions, while when offers more complex branching with multiple cases. Both constructs facilitate executing different code paths based on specified conditions, enhancing the flexibility and control of program flow. Conditional statements are like the choices and decisions you make in your daily life. If it's raining, you take an umbrella; if it's sunny, you wear sunglasses. Similarly, in programming, you use conditionals to make your code smart enough to make choices and adapt to different situations, enhancing its usefulness and flexibility.

Introduction to Conditional Statement

In programming languages like Kotlin, there are two main types of conditional statements: if and when. The if statement in kotlin is like a traffic signal for your program. It checks if a certain condition is true, and if it is, the program takes one path; if not, it takes another. The when statement, on the other hand, is like a multiple-choice question for your code. It provides a way to consider various options and execute different code blocks based on the value you provide. Conditional statements in Kotlin, like if,else if, and else, are vital for crafting responsive and adaptable programs. Conditional statements are essential for creating responsive and adaptable programs.

if Statement 

If statement in kotlin is a fundamental tool for making decisions in your code. It's like asking a question and taking different actions based on the answer. The basic structure looks like this:

Here's an example of If statement in kotlin:

This example checks if the isRaining variable is true (which means it's currently raining) and if it is, it advises you to take an umbrella. If the variable were false, the code inside the "if" block would be skipped, and no message would be printed. This is a simple example of using an if statement in kotlin to make a decision based on a condition.

Kotlin if-else Statement

The Kotlin if-else statement is an extension of the basic "if" statement and provides a way to handle both the true and false cases of a condition. It allows your program to make one choice if a condition is true and a different choice if the condition is false.

Here's how the if-else statement works:

Here's an example: In this example, if the user's age is 20, the program will print the message welcoming them. If the age is below 18, it will print the message indicating they are not old enough.

Kotlin if-else-if Statement

The Kotlin if-else-if statement is an expansion of the "if-else" statement and is used when you have multiple conditions to consider. It allows you to check multiple conditions one after another and execute corresponding code blocks based on the first condition that is true. It's generally advisable to include an else block when using conditional statements in Kotlin. It's like having a backup plan. If none of the conditions you set up are true, the else block kicks in and handles the situation.

Here's how the if-else-if statement works:

Imagine you want to categorize a given number as positive, negative, or zero. Here's an example using the if-else-if statement:

Kotlin When Expression

The Kotlin when expression is a versatile construct that allows you to replace complex if-else-if chains and switch statements with a more concise and expressive syntax. It's used to handle multiple cases and execute corresponding code blocks based on the value of an expression.

Here's how the when expression works:

Here's an example using the when expression: In this example, the program checks the value of dayOfWeek and executes the code block corresponding to the matching case. Since dayOfWeek is 3, the program will print "It's Wednesday."

The when expression is a powerful tool in Kotlin. It's not just about checking values; it can handle more complex situations too. You can use it to evaluate expressions, check ranges of values, and respond to a variety of conditions. This makes your code even more adaptable and capable of handling intricate decision-making, beyond simple value comparisons. Here's an example that demonstrates the flexibility of the when expression in Kotlin:

Ouput:

In this example, we're using the when expression to evaluate different conditions for the value of x. Instead of just checking for specific values, we're also using ranges to determine the appropriate response.

Conclusion

  • Conditional statements in Kotlin are essential for creating flexible and responsive programs by allowing code execution based on specified conditions.
  • The if statement enables simple decision-making by evaluating a condition and executing code if it's true, with optional else blocks to handle the false case.
  • The when statement offers a versatile replacement for traditional switch statements, accommodating multiple cases and complex conditions.
  • When expressions can include ranges, expressions, and various conditions, providing a more powerful and concise way to handle branching logic.
  • Both "if" and when constructs contribute to code readability and maintainability, enhancing the clarity of decision points in your program.
  • Conditional statements empower developers to create applications that respond intelligently to changing data, user interactions, and varying scenarios.