Home »
Kotlin
Conditional Statements in Kotlin programming language
By IncludeHelp Last updated : December 04, 2024
Conditional statements are statements that are used in making decisions. These statements decide what code to run when the condition is true and when the condition is false.
Conditional statements in Kotlin are:
- if statement
- if-else statement
- if-else as expression
- when statement
if statement
if statement checks the condition first, if the condition is true then code that follows the if statements will execute.In Kotlin if condition can be used as an expression means it can return a value.
Syntax
var max=a
if(b>a)
max=b
if-else statement
if statement only lets you to execute code when the condition is true, but what when if condition is false. In such casewe need else statement. So when if the condition is false , else block will be executed.
Syntax
if(a>b)
max=a
else
max=b
if-else as an expression
if-else statement as expression works similar like ternary operator. If the condition is true it will return first value and if condition is false it will return else part.
Syntax
var max = if(a>b) a else b
Note: If, if/else branch can be contained in the blocks then the last statement of the block is considered as value of the block.
Syntax
var max= if(a>b) {
println("a is max")
a
}
else{
println("b is max")
b
}
when statement
When we have to select one choice from multiple choices, then instead of using several if..else..if.. Statements ( if-else ladder) we can use when statement. When matches its arguments to all the test condition until argument matches to any given case. It can be used as a statement or as an expression.
This is a replacement of switch block i.e. present in c or java. when functions similar to switch block but with better functionality. In Kotlin we use when block as
Syntax
when(a){
1 ->print("x=1")
2 ->print("x=2")
3,4 ->print("x is 3 or 4") //if multiple case to be handle in same way
in 5..10 ->print("in rage of 5 to 10")
else ->print("not in range 1 to 10 ")
}
There is no need of break statement as it automatically dismisses the when block after executing the statements in the matching test condition.