5 Conditional constructs and loops

 

After reading this lesson, you’ll be able to

  • Control the execution of your code using an if-else construct
  • Iterate through instructions using while and for loops

In lesson 4, you learned how to define values and variables to store 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 constructs and loops in Scala. You’ll use if-else constructs to check if the system should allow a requested operation in the capstone.

5.1 If-else construct

Suppose you have a list of events; each one has a number representing the day of the week it occurs, starting with 1 for Sunday and ending with 7 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.

5.2 While loop

5.3 For loop

Summary

Answers to quick checks