5 Conditional Statements and Loops
After reading this lesson, you’ll be able to:
- Control the execution of your code using an if-else statement.
- Iterate through instructions using while and for loops
In lesson 4, you have learned how to define values and variables to store some computation results. But life is not always so linear: when performing a task, you may also need to make informed decisions by selecting one approach rather than the other. In this lesson, you’ll discover how to combine different executions of your code through given conditions. You’ll learn how to use the most common conditional statements and loops in Scala. You’ll use if-else statements to check if the system should allow a requested operation in the capstone.
5.1 If-else statement
Suppose you have a list of events: each one has a number representing the day of the week it occurs, starting with one for Sunday and ending with seven for Saturday. You want to write a function that takes an integer representing a day of the week and returns either “weekday” or “weekend” accordingly. For example, you could come up with a solution similar to the following: