Sitemap
Oluwatosinodejide

Oluwatosinodejide

Mastering Control Flow in Kotlin — Conditionals and Loops in My Android Development Journey with Meta

5 min readOct 17, 2026

--

Stylized Kotlin Logo

Welcome back to Week 5 of my DevCareerXCoursera Learning for Impact Scholarship Program Cohort 2 journey.

Last week, we laid the groundwork by mastering variables, data types, and Boolean logic. But knowing how to store data is only half the battle. To build truly functional applications, our code needs to be able to make decisions and automate repetitive tasks.

This week, in Meta’s Introduction to Programming in Kotlin course, we looked into Control Flow, tackling both Conditionals and Loops simultaneously. I learned how to guide the execution path of a program, transforming static code into dynamic, intelligent, and efficient applications.

Here is a comprehensive breakdown of the key concepts I learned, complete with practical code examples.

Conditionals: The Decision Makers

Conditional statements are the traffic controllers of a program. They help control the flow of execution based on specific conditions, ensuring the right code runs at the right time.

The Basics: if and if-else statement
The ifstatement executes a block of code ONLY IF a specified condition evaluates to true. When we have two possible scenarios, the `if-else` statement allows us to handle both gracefully.

One of Kotlin’s most elegant features is that if-else can be used as an expression. This means you can assign the result of an if-else block directly to a variable. If the body contains only a single expression, the curly braces can be omitted for much cleaner, concise code.


val isRaining = false
// Instead of bulky braces, we assign the result directly
val activity = if (isRaining) "Read a book" else "Go for a walk"
println(activity) // Output: Go for a walk

The “when”expression
While if-else-if chains work, Kotlin offers a much more powerful, readable, and versatile alternative: the whenstatement. It is quite similar to the switch or match statement in other languages, but supercharged.

Get Oluwatosinodejide’s stories in your inbox

Join Medium for free to get updates from this writer.

The when statement can be used in several powerful ways:
1. Matching specific values: Comparing a subject variable against multiple possibilities.

fun main() {
println("Is it going to rain?")
val probability = 70

when {
probability < 40 -> {
println("Unlikely")
}

probability <= 80 -> {
println("Likely")
}

probability < 100 -> {
println("Yes")
}

else -> {
println("What?")
}
}
}

2. As an expression: Returning a value directly into a variable, making code highly declarative.

fun main() {
val diceRoll = 3

val result = when (diceRoll) {
1 -> "Missed hit"
in 2..5 -> "Hit with value $diceRoll" // Using Kotlin's range operator
6 -> "Critical hit"
else -> "Unsupported value"
}

println(result) // Output: Hit with value 3
}

3. Smart Type Checking: Using the isoperator to check a variable’s type. Kotlin’s compiler is smart enough to perform “Smart Casts” automatically treating the variable as that type inside the specific branch.

fun main() {
val something: Any = "ABC" // or 123, 0.1, true
when (something) {
is String -> println("This is String")
is Int -> println("This is Int")
is Double -> println("This is Double")
is Boolean -> println("This is Boolean")
}
println(something)
}

Loops: The Engines of Automation

Loops allow the repeated execution of a block of code as long as a condition remains true. They are essential for code reuse, whether running a game loop until the game ends, iterating over elements to display offers on a website, or continuously reading sensor data to control a drone.

  1. The whileLoop:
    The while loop checks its condition (predicate) before each iteration. If the condition is true, the body executes repeatedly until the condition becomes false. If it is false initially, the body is never executed.

NOTE: Care must be taken to update the condition inside the loop to avoid infinite loops!

fun main() {
var printedTimes = 0

while (printedTimes <= 2) {
println("Line $printedTimes")
printedTimes += 1 // Crucial: updating the condition to avoid an infinite loop
}
}

2. The forLoop and Kotlins’ special Ranges ..
The for-loop iterates over collections or number ranges using the syntax: for (variable in collection) { body }. Kotlin makes iterating over numbers highly readable with elegant range operators:
* ..: Inclusive range (includes both start and end numbers).
* until: Exclusive range (includes the start number but excludes the end number).
* downTo: Iterates backwards from a higher number to a lower number.
* step: Defines custom increments or decrements between iterations.

fun main() {
// Inclusive range
for (i in 1..3) {
print("$i ") // Output: 1 2 3
}
println()

// Exclusive range
for (i in 1 until 3) {
print("$i ") // Output: 1 2
}
println()

// Backwards with custom step
for (i in 5 downTo 1 step 2) {
print("$i ") // Output: 5 3 1
}
println()
}

Nested Loops: Building Complexity

A nested loop is simply a loop placed inside another loop. The outer loop controls the “rows,” and for each iteration of the outer loop, the inner loop runs completely, controlling the “columns.”

While this might sound abstract, it is incredibly versatile and useful for generating complex 2D patterns, such as grids or triangles, by manipulating row and column logic.

Code Example: Building a Star Triangle

fun main() {
val height = 5

// Outer loop controls the rows (1 to 5)
for (i in 1..height) {
// Inner loop prints stars. The number of stars equals the current row number.
for (j in 1..i) {
print("*")
}
// Move to the next line after each row is complete
println()
}
}

/* Output:
*
**
***
****
*****
*/

By adjusting the inner loop’s logic (for example, using `height downTo 1` or calculating spaces), we can easily transform this into an upside-down triangle or a centered pyramid.

fun main() {
val height = 10
for (numberOfStars in height downTo 1) {
for (j in 1..numberOfStars) {
print("*")
}
println()
}
}

/* Output

**********
*********
********
*******
******
*****
****
***
**
*

*/

Final Thoughts: Solidifying the Foundation

Looking back at Week 5, my biggest takeaway is that control flow is the absolute heartbeat of application logic. Whether I am validating a user’s password strength, determining UI visibility based on user roles, automating repetitive tasks, or generating dynamic content, clean conditional and loop logic is what separates a coder who writes spaghetti code from a true software developer who writes maintainable, professional-grade applications.

My next step is to keep practicing these concepts by building small algorithmic challenges until I can apply them independently and effortlessly in real Android projects!

My goal remains the same: competence, practical experience, and job readiness.

Still learning. Still building. Still improving.

#MobileDevelopment #Kotlin #AndroidDevelopment #Meta #Coursera #DevCareerXCoursera #DCLearning4Impact #SoftwareEngineering

--

--