KtDevLog
  • Home
  • Kotlin Fundamentals
  • App Projects
  • Android Studio
  • Firebase
No Result
View All Result
KtDevLog
  • Home
  • Kotlin Fundamentals
  • App Projects
  • Android Studio
  • Firebase
No Result
View All Result
KtDevLog
No Result
View All Result
Kotlin Control Flow: If, Else, and When expressions.

Kotlin Control Flow: If, Else, and When expressions.

Md Sharif Mia by Md Sharif Mia
April 10, 2026
in Kotlin Fundamentals
Reading Time: 10 mins read
0
0
Share on FacebookShare on Twitter

If, Else statement

Kotlin
val age = 20
if (age >= 18) {
    println("Welcome to KtDevLog!")
} else {
    println("Sorry, you are too young.")
}
KtDevlog

Imagine, on your apps have many users you want to add filter or logic for allowing the access. So you think you only access the if the user age 18 or above 18. Then your can used this statement. if statement used for comparison. And else statement executes a block of code only when the preceding if condition is false.

when

Imagine you are at a vending machine. You press a number, and the machine “matches” that number to a specific snack. That is exactly what when does.

Kotlin
val dayNumber = 3

val dayName = when (dayNumber) {
    1 -> "Monday"
    2 -> "Tuesday"
    3 -> "Wednesday"
    else -> "Other day" // The 'else' acts like a fallback
}

println(dayName) // Output: Wednesday
KtDevlog

Comparison: if-else vs when

Featureif-elsewhen
ReadabilityBecomes messy with many conditions.Very clean and organized.
LogicBest for True/False decisions.Best for multiple choice options.
FlexibilityStandard logic.Can check types, ranges, and conditions.

Related Posts

kotlin Data Types

kotlin Data Types

April 10, 2026
Kotlin Variables val vs var

Kotlin Variables val vs var

April 10, 2026
Hello World with Kotlin

How to Write a Kotlin Hello World (A Beginner’s Guide)

April 11, 2026
ShareTweetPin
Md Sharif Mia

Md Sharif Mia

Related Posts

kotlin Data Types
Kotlin Fundamentals

kotlin Data Types

April 10, 2026

Numbers Kotlin provides several built-in types to represent numbers, categorized into integers and floating-point...

Kotlin Variables val vs var
Kotlin Fundamentals

Kotlin Variables val vs var

April 10, 2026

Val val name = "Sharif"val name = "Sharif" Use val for a variable whose value never...

Hello World with Kotlin
Kotlin Fundamentals

How to Write a Kotlin Hello World (A Beginner’s Guide)

April 11, 2026

Have you ever stared at a blank computer screen, wishing you knew how to...

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About
  • FAQ
  • Contact
  • Advertise

© 2026 ktdevlog.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • Kotlin Fundamentals
  • App Projects
  • Android Studio
  • Firebase

© 2026 ktdevlog.