Blog: Mastering the Flow in Kotlin
Day 5! Today was all about Control Flow. If you’re coming from Java, Kotlin’s approach to if and when will change how you write code forever.
if is a Value
In Java, if is just a command. In Kotlin, it’s a result! I can assign the result of an if block directly to a variable. No more ternary operators; just clean, readable logic.
The Mighty when
The when expression is hands-down my favorite feature so far. It’s like a switch statement on steroids. You can check strings, numbers, or even complex boolean conditions. It forces you to handle the else case if you're using it as an expression, which makes your code much more robust.
Ranges: The .. Magic
Creating a loop for numbers 1 to 10 is as simple as for (i in 1..10). Want to skip numbers? Just add step 2. Want to count down? Use downTo. It’s incredibly expressive.
FizzBuzz in 5 Lines
Using when without a subject allowed me to write a super clean FizzBuzz implementation. Instead of nested if-else blocks, each condition is clearly laid out on its own line.
Kotlin is making coding feel less like "writing instructions" and more like "describing logic."
Check out the full technical breakdown!
Comments
Post a Comment