Welcome, learners! Let's dive deeper into Kotlin programming by combining loops and decision-making structures. We'll explore the power of integrating loops, like for
and while
, with conditional structures such as if
and when
. Such combinations can make our programs significantly more dynamic and flexible.
We'll revisit the conditional constructs in Kotlin. You'll recall the if
construct, which executes a block of code when a condition is met. For example:
Kotlin1val number = 10 2if (number > 0) { 3 println("The number is positive.") // This line is executed if the condition is true 4}
Compound conditions, built with logical operators like &&
(AND) and ||
(OR), can also be used:
Kotlin1val number = 10 2if (number > 0 && number <= 10) { 3 println("The number is positive and less than or equal to 10.") // This line is executed if both conditions are true 4}
Next, let's review the when
construct. Like a compact form of if
-else
chains, when
matches its argument with different conditions:
Kotlin1val number = 1 2when (number) { 3 1 -> println("One") // This line is executed because number equals 1 4 2 -> println("Two") 5 else -> println("Invalid number") 6}
We can merge conditional statements with loops. Here's an if
construct within a for
loop:
Kotlin1for (i in 1..10) { 2 if (i % 2 == 0) { 3 println("$i is even.") // If i is divisible by 2, prints that i is even 4 } else { 5 println("$i is odd.") // Otherwise, prints that i is odd 6 } 7}
A for
loop can also easily incorporate a when
construct:
Kotlin1for (i in 1..3) { 2 when (i) { 3 1 -> println("One") // For i equals 1, prints "One" 4 2 -> println("Two") // For i equals 2, prints "Two" 5 else -> println("Three") // For other values of i, prints "Three" 6 } 7}
We can apply the same combined approach with while
loops. Here's a while
loop with an if
construct:
Kotlin1var i = 1 2while (i <= 10) { 3 if (i % 2 == 0) { 4 println("$i is even.") // If i is divisible by 2, prints that i is even 5 } else { 6 println("$i is odd.") // Otherwise, prints that i is odd 7 } 8 i++ // Increase i by 1 after each iteration 9}
A while
loop can also include a when
construct:
Kotlin1var i = 1 2while (i <= 3) { 3 when (i) { 4 1 -> println("One") // For i equals 1, prints "One" 5 2 -> println("Two") // For i equals 2, prints "Two" 6 else -> println("Three") // For other values of i, prints "Three" 7 } 8 i++ // Increase i by 1 after each iteration 9}
Now, let's consider real-life examples. Imagine you're organizing a kids' game. Each child has cards numbered from 1
to 10
. If the card number is even, it's placed in Box A; otherwise, it's placed in Box B.
Kotlin1for (card in 1..10) { 2 if (card % 2 == 0) { 3 println("Place card $card in Box A") // For even number cards 4 } else { 5 println("Place card $card in Box B") // For odd number cards 6 } 7}
Consider a scenario of a smart home garden watering system. Flowers are watered on Monday, Wednesday, and Friday, whereas trees are watered on Tuesday and Thursday:
Kotlin1var dayOfWeek = 1 2while (dayOfWeek <= 5) { 3 when (dayOfWeek) { 4 1, 3, 5 -> println("Water the flowers.") // Water flowers on day 1, 3, and 5 5 2, 4 -> println("Water the trees.") // Water trees on day 2 and 4 6 else -> println("Invalid day.") // Prints for an invalid day 7 } 8 dayOfWeek++ // Increase day of week by 1 after each cycle 9}
By slightly extending our basic understanding of loops and conditions, we can create intricate and useful code!
We've reviewed the if
and when
constructs and have learned how to use these within for
and while
loops. Coming next are practice exercises where you'll apply these concepts. Remember— the best way to learn is by doing. Good luck!