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:

val number = 10
if (number > 0) {
    println("The number is positive.") // This line is executed if the condition is true
}

Compound conditions, built with logical operators like && (AND) and || (OR), can also be used:

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

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:

val number = 1
number match {
    case 1 => println("One") // This line is executed because the number equals 1
    case 2 => println("Two")
    case _ => println("Invalid number")
}

Conditional Constructs within For Loop

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

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

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

for (i <- 1 to 3) {
    i match {
        case 1 => println("One") // Prints "One" if i equals 1
        case 2 => println("Two") // Prints "Two" if i equals 2
        case _ => println("Three") // Prints "Three" for other values of i
    }
}

Conditional Constructs in While Loop

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

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

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

var i = 1
while (i <= 3) {
    i match {
        case 1 => println("One") // Prints "One" if i equals 1
        case 2 => println("Two") // Prints "Two" if i equals 2
        case _ => println("Three") // Prints "Three" for other values of i
    }
    i += 1 // Increase i by 1 after each iteration
}

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.

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

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:

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

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!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal