Deep Kotlin tutorial covering if expression, when expression, for loop, while loop with examples, diagram, mistakes, and interview answers.
if, when, Loops, and Ranges
What is if, when, Loops, and Ranges?
if, when, Loops, and Ranges is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: if expression, when expression, for loop, while loop, ranges. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
Why students should learn it
- Understand if, when, Loops, and Ranges in simple language
- Use if expression in real Kotlin or Android code
- Explain if, when, Loops, and Ranges in interviews with examples and trade-offs
How it works
if, when, Loops, and Ranges: if expression, when expression, for loop, while loop, ranges works through Kotlin's core syntax rules. The compiler infers types where it can, expressions such as if and when can produce a value directly instead of only running a side effect, and functions are written to be small and named so the intent is obvious from the signature without reading the body.
- Identify what problem if, when, Loops, and Ranges: if expression, when expression, for loop, while loop, ranges solves.
- Write the smallest Kotlin snippet that demonstrates it.
- Add one Android or JVM use case where it matters.
- Compare it with a similar concept so the difference is clear.
- Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.
Types / Classification
- if expression: core concept in this lesson
- when expression: core concept in this lesson
- for loop: core concept in this lesson
- while loop: core concept in this lesson
- ranges: core concept in this lesson
- Statement: performs an action without producing a value
- Expression: if/when/try can produce a value directly
- Declaration: val/var/fun/class introduces a new name
Important differences
| Concept 1 | Concept 2 | How to remember it |
|---|---|---|
| if statement | if expression | A statement just runs code; an expression also produces a value you can assign or return. |
| Positional argument | Named argument | Positional relies on order; named argument is explicit and safer with many parameters. |
| Function | Lambda | A function has a name and is called by that name; a lambda is an anonymous function value you can pass around. |
Diagram
flowchart TD
A["1.1 Kotlin Language Fundamentals"] --> B["if, when, Loops, and Ranges"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]
Examples
Named and default parameters
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")
when as an expression
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}
When to use it
Use it when: Reach for this when it makes your code clearer, safer, or easier for the next developer to follow.
Avoid it when: Skip it when a simpler, more familiar approach already solves the problem without extra ceremony.
Common mistakes
- Writing long positional parameter lists instead of using named arguments for clarity
- Using var when the value never actually changes
- Copy-pasting if/else chains instead of a single when expression
- Ignoring type inference and writing redundant explicit types everywhere
- Writing a multi-line lambda where a named function would read better
Interview questions
Read each answer in four parts: simple answer, code example, trade-off, and common mistake.
1. What is if, when, Loops, and Ranges in 1.1 Kotlin Language Fundamentals?
Answer: if, when, Loops, and Ranges is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: if expression, when expression, for loop, while loop, ranges. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
2. How does if, when, Loops, and Ranges work in Kotlin or Android code?
Answer: if, when, Loops, and Ranges: if expression, when expression, for loop, while loop, ranges works through Kotlin's core syntax rules. The compiler infers types where it can, expressions such as if and when can produce a value directly instead of only running a side effect, and functions are written to be small and named so the intent is obvious from the signature without reading the body.
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Using var when the value never actually changes
3. What is one common mistake with if, when, Loops, and Ranges?
Answer: A common mistake is: Writing long positional parameter lists instead of using named arguments for clarity Another is: Using var when the value never actually changes
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
4. How would you explain if, when, Loops, and Ranges with a small example?
Answer: if, when, Loops, and Ranges is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: if expression, when expression, for loop, while loop, ranges. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Using var when the value never actually changes
5. What is the trade-off of using if, when, Loops, and Ranges?
Answer: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
Practice task
Write one Kotlin example for if, when, Loops, and Ranges. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.