What’s in it for you ?
Conditionals in Kotlin
Different types of if else expressions
If expression
If else expressions
If else if ladder expressions
Nested if expression
if
If is used when a block of code
must be executed based on the
condition to be true
else
else is used when a block of
code must be executed if the
same condition is false
else if
else if is used to specify a new
condition if the previous condition
is false
Conditionals in Kotlin
Different types of if else expressions
If expression
If else expression
If else if ladder expression
Kotlin Nested If expression
01
02
03
04
02
03
04
02
03
01
02
03
04
if expressions
The if expression is used to check the condition, if the
condition is true then only the code mentioned inside the if
block will execute.
If code
False
True
Exit
Start
Condition
if else expressions
In if-else expression, there are two blocks of statements,
the if block executes when the condition is true, and if
the condition is false, then the else block executes.
In if-else expression, there are two blocks of
statements, the if block executes when the condition is
true, and if the condition is false, then the else block
executes.
If block
False
True
Exit
Start
Condition
else block
if else if ladder expressions
Kotlin if else if condition is used when there are multiple
conditions to be checked.
In this expression first the if expression is checked if its true
then it executes that if block otherwise it moves to other else if
conditions.
If none of the condition turns out to be true then it executes the
last final else expression.
fun main()
{
val marks = 117
val result = if (marks < 105 && marks
>80)
{
"Cutoff not cleared( 2nd exam)"
}
else if (marks in 105..130)
{
"Cutoff cleared"
}
else if (marks in 130..180)
{
"Cutoff cleared (No fee)"
} else {
“Cutoff not cleared"
}
println(result)
}
Nested if expressions
In Kotlin nested if expression, there are if statements inside another if
statement.
First the if expression is checked if it is true then the nested if
condition is checked. If the condition turns out to be false then it will
move to the else expression.
fun main()
{
val num = 30
if(num>0)
{
if(num%2 == 0)
println("Even Number")
else
println("Odd Number")
}
else {
println("Negative number")
}
}

Kotlin If Else Statement Explained

  • 2.
    What’s in itfor you ? Conditionals in Kotlin Different types of if else expressions If expression If else expressions If else if ladder expressions Nested if expression
  • 3.
    if If is usedwhen a block of code must be executed based on the condition to be true else else is used when a block of code must be executed if the same condition is false else if else if is used to specify a new condition if the previous condition is false Conditionals in Kotlin
  • 4.
    Different types ofif else expressions If expression If else expression If else if ladder expression Kotlin Nested If expression 01 02 03 04 02 03 04 02 03 01 02 03 04
  • 5.
    if expressions The ifexpression is used to check the condition, if the condition is true then only the code mentioned inside the if block will execute. If code False True Exit Start Condition
  • 6.
    if else expressions Inif-else expression, there are two blocks of statements, the if block executes when the condition is true, and if the condition is false, then the else block executes. In if-else expression, there are two blocks of statements, the if block executes when the condition is true, and if the condition is false, then the else block executes. If block False True Exit Start Condition else block
  • 7.
    if else ifladder expressions Kotlin if else if condition is used when there are multiple conditions to be checked. In this expression first the if expression is checked if its true then it executes that if block otherwise it moves to other else if conditions. If none of the condition turns out to be true then it executes the last final else expression. fun main() { val marks = 117 val result = if (marks < 105 && marks >80) { "Cutoff not cleared( 2nd exam)" } else if (marks in 105..130) { "Cutoff cleared" } else if (marks in 130..180) { "Cutoff cleared (No fee)" } else { “Cutoff not cleared" } println(result) }
  • 8.
    Nested if expressions InKotlin nested if expression, there are if statements inside another if statement. First the if expression is checked if it is true then the nested if condition is checked. If the condition turns out to be false then it will move to the else expression. fun main() { val num = 30 if(num>0) { if(num%2 == 0) println("Even Number") else println("Odd Number") } else { println("Negative number") } }