Welcome! Today, we're delving into the world of conditional statements in programming through the lens of Scala. To relate this to a real-world example, consider a traffic signal that shows different colors to manage vehicular flow — green for go, and red for stop. Similarly, Scala uses conditional statements to control the flow of execution in programs.
In Scala, we utilize if
and match-case
expressions to aid in decision-making. Here is an example of a simple if
statement:
Indentations matter in Scala, so ensure your code blocks are correctly indented to maintain readability and structure.
In Scala, the if
statement is utilised to evaluate a condition. If the condition holds true, a specific block of code is executed. Consider an example where we verify whether a person with the age
of 20 is an adult:
In this instance, "You are an adult." is printed because our age
, 20, is greater than or equal to 18.
Scala provides constructs like if-else
and if-else-if-else
, which enable the conditional execution of code blocks based on various conditions. For instance, consider a game where players win a prize if they score more than 70 points; those who score 70 or less don't win:
For more complex scenarios, such as a quiz game in which different prizes (bronze, silver, gold) are won based on scores, you would use the if-else-if-else
construct:
This example illustrates how Scala evaluates multiple conditions in an if-else-if-else
structure, selecting and executing the first true condition, while ignoring the rest.
A key feature of Scala is the ability to use if-else
as an expression. This enables you to return a value from a conditional check directly to a variable. Let's examine how this works:
In this example, the if-else
expression evaluates a condition and assigns the result directly to a variable. This makes your code more concise and enhances readability.
Scala's match-case
expression allows for clean and readable conditional logic. It sequentially evaluates conditions and executes the code block of the first matching branch, serving as an effective mechanism for handling multiple conditional paths.
Scala enables the definition of multiple conditions within a single branch of a match-case
expression:
In this code, participants can win a grand prize either by having a score greater than 90 or by having a lucky participant number of 1000. Even if their score is less than 90, they can still win the grand prize because of their lucky number. If the score is greater than 70 but doesn't qualify for the grand prize, they win a silver prize. All other cases receive a participation prize.
You can use the match-case
expression to compare a variable against predefined values or conditions. For instance, to check if a number is odd or even:
In Scala, match-case
not only controls flow, but it can also be used as an expression to return values:
Congratulations on completing this lesson. You've learned about if
and match-case
expressions in Scala, and how they're applied to manage decision-making and flow control. Next, you'll have an opportunity to apply what you've learned through hands-on exercises. Practice is key in programming. Keep up the good work!
