Kotlin If … Else

Kotlin If…Else – Complete Beginner Guide With Examples
Decision-making is a fundamental part of programming. In Kotlin, the if…else statement allows your program to execute different blocks of code based on conditions.
Whether you’re building Android apps, backend services, or learning Kotlin as a beginner, understanding if…else is essential.
In this beginner-friendly guide, you will learn:
What
if…elseis in KotlinBasic syntax
If as a statement vs expression
Using multiple conditions
Nested if statements
Real-world examples
Common beginner mistakes
Best practices
Let’s get started
What Is If…Else in Kotlin?
The if…else statement is used to make decisions in your program.
It checks a condition:
If the condition is true, one block runs.
If the condition is false, another block runs.
Basic Syntax of If Statement
Simple Example
Output:
If…Else Statement
When you want an alternative block of code, use else.
Example
If…Else If…Else Ladder
When multiple conditions need checking:
Example
If as an Expression in Kotlin
Unlike Java, Kotlin’s if can return a value.
Example
Output:
This makes Kotlin more concise.
Multi-Line If Expression
Using Logical Operators in If
Combine conditions using:
&& (AND)
|| (OR)
! (NOT)
Example
Nested If Statement
You can place an if inside another if.
Example
Use nested if carefully to maintain readability.
Real-World Example – Login Validation
Real-World Example – Even or Odd
If vs When in Kotlin
| Feature | if | when |
|---|---|---|
| Simple conditions | Best choice | Possible |
| Multiple values | Less readable | Better |
| Range checking | Manual | Built-in |
| Type checking | Limited | Built-in |
Use if for simple checks.
Use when for multiple branches.
Null Safety with If
Or use:
Boolean Conditions in If
Common Beginner Mistakes
Using = Instead of ==
Wrong:
Correct:
Forgetting Curly Braces
Always use braces for clarity.
Writing Complex Conditions
Break long conditions into readable parts.
Best Practices
- Keep conditions simple
- Use meaningful variable names
- Avoid deep nesting
- Use when for multiple branches
- Prefer if expression when returning value
Performance Considerations
If statements are lightweight
Avoid complex nested conditions
Use short-circuit logic wisely
When Should You Use If…Else?
Use if…else when:
Checking simple conditions
Comparing values
Validating user input
Handling authentication
Making decisions in loops
Frequently Asked Questions (FAQs)
1. What is if statement in Kotlin?
The if statement allows conditional execution of code.
2. Can if return a value in Kotlin?
Yes, It supports if as an expression.
3. What is the difference between if and when?
If is best for simple conditions. When handles multiple branches better.
4. Can I nest if statements?
Yes, but avoid deep nesting.
5. Do I need parentheses in Kotlin if?
Yes, conditions must be inside parentheses.
Conclusion
The Kotlin if…else statement is a powerful decision-making tool.
You learned:
Basic syntax
If as expression
Logical operators
Nested if
Real-world examples
Best practices
Mastering if…else will improve your Kotlin programming skills significantly.
