Theory
Decisions that hand back a value
FestConnect Mobile must classify events: an event with 0 seats is 'Sold out', 1 to 50 is 'Filling up', more is 'Available'. In Java (BCA403) you would write an if-else chain or a switch with breaks.
Kotlin does this more elegantly, and with two twists that surprise Java programmers: its if and when are EXPRESSIONS that RETURN a value, and its when (the switch replacement) can match RANGES and TYPES, not just single values. This lesson covers Kotlin conditionals and ranges, where the language shows its conciseness.
Theory
if as an expression
Kotlin's if works as a statement (as in Java) AND as an expression that returns a value:
val max = if (a > b) a else b
The whole if evaluates to a value and is assigned. This REPLACES Java's ternary operator (a > b ? a : b): Kotlin has no ternary because if already does the job, more readably. The branch that runs supplies the value.
This expression nature is a theme in Kotlin: constructs that were mere statements in Java often return values here, letting you write val result = if (...) ... else ... cleanly.
Practical
when with ranges, and if as expression
fun classify(seats: Int): String {
// when: a supercharged switch, also an expression (returns a value)
return when (seats) {
0 -> "Sold out" // single value
in 1..50 -> "Filling up" // a RANGE (1 to 50 inclusive)
in 51..200 -> "Available"
else -> "Plenty of seats" // else (like default)
}
}
fun main() {
println(classify(0)) // Sold out
println(classify(30)) // Filling up (30 is in 1..50)
println(classify(500)) // Plenty of seats
// if as an expression (replaces Java's ternary):
val a = 7; val b = 4
val max = if (a > b) a else b
println(max) // 7
}
Theory
when and ranges
when is Kotlin's switch, but far more capable, and needs NO break:
- match a single value:
0 -> ... - multiple values:
1, 2 -> ... - a RANGE:
in 1..50 -> ... - a type:
is String -> ... else(like default)
And when is an EXPRESSION, so it can return a value directly (the return when in the listing).
Ranges use ..: 1..10 is 1 to 10 INCLUSIVE. They appear in when (in 1..50), in loops (for (i in 1..5)), and in conditions (if (x in 1..10)). Variants: until (exclusive end, 1 until 10 is 1 to 9), downTo (counting down), step. Ranges make numeric conditions read almost like English: if (seats in 1..50).
Quiz
How does Kotlin's when differ from Java's switch?
- when needs a break after every branch, like switch
- when needs NO break, can match ranges (in 1..50) and types (is String), and is an EXPRESSION that returns a value
- when can only match single integer values
- when and switch are identical
Show the answer
when needs NO break, can match ranges (in 1..50) and types (is String), and is an EXPRESSION that returns a value
Kotlin's when is a more powerful switch: it needs NO break (no fall-through to guard against, unlike Java's switch which needed break on every case, BCA403), it can match not just single values but RANGES (in 1..50), TYPES (is String), and multiple values (1, 2 ->), and it is an EXPRESSION so it can return a value directly (val x = when(...)). Option A wrongly imports Java's break requirement. Option C undersells when's rich matching (ranges, types). Option D denies the real differences. So when is cleaner and more capable than switch, replacing long if-else chains elegantly, and the no-break design removes Java's classic forgotten-break bug.
Think first
if or when: which for FestConnect?
FestConnect Mobile needs (a) to pick the larger of two seat counts, and (b) to classify an event into one of five categories by seat range. Which conditional fits each, and why? Then tap.
Show the answer
(a) Picking the larger of TWO values is a simple two-way decision: use IF (as an expression): val max = if (a > b) a else b. Clean and direct. (b) Classifying into FIVE categories by seat RANGE is a multi-way match on one value: use WHEN with ranges: when(seats) { in 0..0 -> ...; in 1..50 -> ...; ... }. A when reads far better than a long if-else-if chain and naturally expresses ranges. The rule of thumb: IF for boolean or two-way decisions; WHEN for matching one value against many cases, especially with ranges or types. Both are expressions, so both can return a value. Choosing the right one keeps the code readable: when a decision has many branches, when almost always wins.
Watch out
Conditional traps
Expecting break in when: there is none; each branch runs alone (no fall-through).
when as expression needs else: when returning a value must be exhaustive; add else (unless all cases are covered, e.g. an enum).
.. vs until: 1..10 includes 10; 1 until 10 stops at 9; pick the right one for boundaries.
Looking for a ternary: Kotlin has none; use if as an expression.
Ranges only for numbers-ish: .. works for comparable types (Int, Char); use it where ordering makes sense.
Theory
Decisions done; now collections
FestConnect Mobile can decide and classify with if, when and ranges. Real apps hold LISTS of data, a list of events, of registrants. The next lesson covers Kotlin's arrays and lists, how to create, access and modify them, including the important read-only vs mutable list distinction that echoes val vs var. Then loops complete Kotlin's fundamentals, and the OOP unit builds the app's classes.
Summary
Key takeaways
- Kotlin's if is both a statement and an EXPRESSION that returns a value: val max = if (a > b) a else b (replaces Java's ternary).
- when is Kotlin's switch replacement: no break needed, and it is also an expression.
- when matches single values, multiple values (1, 2 ->), ranges (in 1..50 ->), and types (is String ->), with else as default.
- Ranges use ..: 1..10 is inclusive; until is exclusive-end (1 until 10 = 1 to 9); downTo counts down; step skips.
- Ranges appear in when, loops (for i in 1..5), and conditions (if (x in 1..10)).
- Use if for two-way/boolean decisions, when for matching one value against many cases (especially ranges/types).
- Memory hook: if and when return values; when needs no break and matches ranges and types.