100 days of Kotlin — Day 4: Decisions, Loops, and the Quest for Efficiency

Rohan Ashlesh
3 min readJun 22, 2023

--

Welcome back to our 100 Days of Learning Kotlin! In Day 3, we explored the fascinating world of variables, where mutability and immutability took center stage. Today, we embark on a quest to master control flow in Kotlin, uncovering the art of decision-making and the magic of loops. So, gear up, fellow Kotlin enthusiasts, and let’s dive right in!

Control Flow: Navigating the Coding Maze

In programming, control flow refers to the order in which statements are executed, allowing us to make decisions and repeat actions based on certain conditions. Kotlin provides us with a variety of control flow statements, enabling us to write efficient and dynamic code. Let’s explore two powerful control flow constructs: decision-making with conditionals and looping for repetitive tasks.

1. Decision-Making: When Choices Shape the Path

Decision-making is like choosing your own adventure — it gives your program the power to take different paths based on certain conditions. In Kotlin, we use conditional statements such as if, else if, and else to make decisions. Let's see an example:

val age = 18

if (age < 18) {
println("Sorry, you're too young for this adventure!")
} else if (age >= 18 && age < 65) {
println("Welcome aboard! Get ready for an exciting journey!")
} else {
println("Ahoy, seasoned adventurer! Enjoy the ride!")
}

In this snippet, we determine the path to take based on the value of the age variable. Whether you're too young, within the prime age range, or a seasoned adventurer, Kotlin's conditionals have got you covered.

A Programmer’s Comedy Club: Conditional Humor

Why did the programmer bring a ladder to the conditional statement? They wanted to climb up the “else if” ladder and reach new programming heights!

2. Looping: A Tale of Repetition

Loops are like magical spells that repeat certain actions, saving us from tedious repetition. Kotlin offers different types of loops, including for and while, to help us unleash the power of repetition. Let's explore a simple example using the for loop:

val favoriteFruits = listOf("apple", "banana", "mango", "kiwi")

for (fruit in favoriteFruits) {
println("I love $fruit!")
}

In this snippet, we iterate over each fruit in the favoriteFruits list and express our love for each one. Loops allow us to automate tasks, process collections, and perform calculations efficiently.

An Inspirational Programming Tale: The Quest for Efficiency

Once upon a time, there was a programmer named Alex. They embarked on a quest to write the most efficient code in the land. Armed with the power of loops, Alex conquered repetitive tasks, saved time, and impressed their fellow coders. The kingdom celebrated Alex’s coding prowess, and the legend of their efficiency spread far and wide!

Remember, fellow programmers, with great control flow comes great responsibility — to write clean, readable, and efficient code.

Congratulations on completing Day 4 of our Kotlin learning journey! Today, we explored the art of decision-making through conditionals and the magic of looping to conquer repetitive tasks. By mastering control flow, we can navigate the coding maze with elegance and efficiency.

Keep coding, laughing, and finding inspiration in the whimsical tales of programming. Remember, the world of tech is not only logical but also full of wonders and surprises!

Wishing you a magical coding experience.

--

--

Rohan Ashlesh
Rohan Ashlesh

Written by Rohan Ashlesh

Product | Consultant | Writer | Business Enthusiast. Anything growth, I'm your guy.

No responses yet