Lesson 3
Scala Control Structures: Integrating Loops with Conditionals
Topic Overview

Welcome, learners! Let's delve deeper into Scala programming by combining loops and decision-making structures. We'll explore the potential of integrating loops, such as for and while, with conditional structures like if and match-case scenarios. These combinations can make our programs considerably more dynamic and flexible.

Understanding Conditional Constructs

Let’s first revisit the conditional constructs in Scala. You'll recall the if construct, which executes a block of code when a condition is met. For example:

Scala
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:

Scala
1val number = 10 2if (number > 0 && number <= 10) { 3 println("The number is positive and less than or equal to 10.") // This line would be executed if both conditions are true 4}

Next, let's review the match-case structure. Resembling a compact form of the if - else chains, the match-case matches its argument with different patterns:

Scala
1val number = 1 2number match { 3 case 1 => println("One") // This line is executed because the number equals 1 4 case 2 => println("Two") 5 case _ => println("Invalid number") 6}
Conditional Constructs within For Loop

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

Scala
1for (i <- 1 to 10) { 2 if (i % 2 == 0) { 3 println(s"$i is even.") // Prints that i is even if i is divisible by 2 4 } else { 5 println(s"$i is odd.") // Otherwise, prints that i is odd 6 } 7}

A for loop can also easily incorporate a match-case structure:

Scala
1for (i <- 1 to 3) { 2 i match { 3 case 1 => println("One") // Prints "One" if i equals 1 4 case 2 => println("Two") // Prints "Two" if i equals 2 5 case _ => println("Three") // Prints "Three" for other values of i 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:

Scala
1var i = 1 2while (i <= 10) { 3 if (i % 2 == 0) { 4 println(s"$i is even.") // Prints that i is even if i is divisible by 2 5 } else { 6 println(s"$i is odd.") // Otherwise, prints that i is odd 7 } 8 i += 1 // Increase i by 1 after each iteration 9}

A while loop can also include a match-case structure:

Scala
1var i = 1 2while (i <= 3) { 3 i match { 4 case 1 => println("One") // Prints "One" if i equals 1 5 case 2 => println("Two") // Prints "Two" if i equals 2 6 case _ => println("Three") // Prints "Three" for other values of i 7 } 8 i += 1 // 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 a card's number is even, it's placed in Box A; otherwise, it's placed in Box B.

Scala
1for (card <- 1 to 10) { 2 if (card % 2 == 0) { 3 println(s"Place card $card in Box A") // For even number cards 4 } else { 5 println(s"Place card $card in Box B") // For odd number cards 6 } 7}

Consider a scenario where a smart home garden watering system is in place. Flowers are watered on Monday, Wednesday, and Friday, while trees are watered on Tuesday and Thursday:

Scala
1var dayOfWeek = 1 2while (dayOfWeek <= 5) { 3 dayOfWeek match { 4 case 1 | 3 | 5 => println("Water the flowers.") // Waters the flowers on days 1, 3, and 5 5 case 2 | 4 => println("Water the trees.") // Waters the trees on days 2 and 4 6 case _ => println("Invalid day.") // Prints for an invalid day 7 } 8 dayOfWeek += 1 // Increase the day of the week by 1 after each cycle 9}

Just 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 match-case structures and have learned how to use these within for and while loops. Up 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.