Conditional statements are statements used in decision-making. These statements determine which code should be executed when the condition is true and when the condition is false.
cda3dr kjhgfdsaasdfghj
Note: Unlike Java, if..else can be used as a statement or an expression (to assign a value to a variable) in Kotlin. See an example at the bottom of the page to better understand this.
The following are the types of conditional statements in Kotlin:
If statement first checks the condition and if the condition is true, the code below if statements will be executed. If the condition can be used as an expression in Kotlin, a value can be returned to it.
If statement condition allows executing the code only if the condition is true, but what if the condition is false. In this case, we need another statement called else; the other block of code will be executed only if the condition is false.
The if-else statement acts as an expression, especially like the ternary operator. If the condition is true, it will give the first value, and if the condition is false, it will give another result.
When multiple choices are there, we can use when statements instead of multiple if..else..if statements.
It replaces the switch block, i.e., In C or Java, functions similar to the switch block but with better performance.
Note: No break statement is required as the block automatically dismisses the statements after being executed in the matching test condition.
|