‫Conditional statements in Kotlin allow you to make decisions in your code based on certain conditions.
‫Let's start with the if statement.
‫The if statement is used to execute a block of code only if a given condition is true.
‫It can be used in two forms, with or without an else part.
‫I'll start with the if statement val age equals to 30.
‫If age greater than 40 or 18.
‫Print.
‫You're an adult.
‫Here.
‫I'm checking if the variable age is greater than 18, and in this case 30 is greater than 18.
‫So we need to go and execute this block of code included inside this curly braces.
‫If we run the program we can see this message you're an adult.
‫If I set the age to 14 then run the program.
‫Nothing displayed because this block of code is escaped because the age is 14 greater than 18.
‫No, this is false.
‫This block of code will be skipped and not executed.
‫The second part of conditional statements is the if else statement.
‫I'll start by creating another variable called score equals to 85.
‫Here I am checking if the score greater than or equal to 60.
‫Then I need to print pass.
‫Otherwise I need to use the else condition here else statement and set to fail.
‫Let me run and see.
‫And you can get pass because this condition is met 85 is greater than or equals to 60.
‫So this block of code will be executed.
‫Let me check and set it to 40.
‫Run again.
‫I need to execute this block of code and display fail.
‫So this is how we use if and else statements.
‫In Kotlin.
‫The when expression is similar to a switch statement in other programming languages.
‫It allows you to evaluate a value against multiple possible cases and execute the code block corresponding
‫to the first matching case.
‫For example, here I'll create val de equals to three when expression.
‫I'm going to check the de against multiple values.
‫So if the de variable equals to one, then print ln that it is Monday do is for Tuesday, three for
‫Wednesday.
‫Also you can use the else expression here print ln unknown de.
‫Run the program, and you can see that this de variable is compared against multiple values.
‫If it is equal to one, then go and print Monday.
‫If it is equal to two, go and print Tuesday if it is equal three and this is the case, go and print
‫Wednesday.
‫Otherwise, if it is not equal to one, two, three, then display unknown day.
‫For example, I set it to five.
‫Run the program and it will display unknown day nested conditions.
‫You can also nest conditional statements to handle complex decision making scenarios, for example.
‫In this example we have two boolean variables, is raining and is cold.
‫It is raining.
‫Yes, it's true, it's cold.
‫No.
‫So it's only raining in this block of code.
‫I am checking here if it is raining.
‫So this is the code that I need to execute and in order to execute it we can see that it is nested conditional
‫statement.
‫So here we have another condition.
‫Inside this condition is raining.
‫So if it is raining yes true.
‫Now go and execute this checking.
‫If it's cold go and execute this code.
‫Otherwise if it is not cold then go and execute this block of code.
‫And in this case it's raining.
‫So it will execute this block of code and is cold is equal to false.
‫So it will not execute this.
‫It will execute this code.
‫And by the way when you write is cold here, then you are checking if it is equal to true.
‫And by default you can omit this.
‫This is a shortcut for this writing.
‫So when you specify the boolean you are inside this these parentheses in the if condition you are checking
‫it.
‫If this is true or not, since it's raining and not cold so it will execute.
‫Use only umbrella.
‫Let me run and see and you can get use only umbrella.
‫Conditional statements are essential for controlling the flow of your program and making decisions based
‫on specific conditions.
‫They allow you to create flexible and responsive code that can adapt to different situations.