Lesson 3
Combining Loops with Conditional Logic in Kotlin
Topic Overview and Actualization

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.

Understanding Conditional Constructs

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:

Kotlin
1val 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:

Kotlin
1val 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:

Kotlin
1val 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}
Conditional Constructs within For Loop

We can merge conditional statements with loops. Here's an if construct within a for loop:

Kotlin
1for (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:

Kotlin
1for (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}
Conditional Constructs in While Loop

We can apply the same combined approach with while loops. Here's a while loop with an if construct:

Kotlin
1var 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:

Kotlin
1var 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}
Real-World Use Cases and Applications

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.

Kotlin
1for (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:

Kotlin
1var 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!

Lesson Summary and Practice

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.