- 01Setting up (what you'll need)
- 02Your first output (println)
- 03Variables (val and var)
- 04Type annotations and basic types
- 05Branching (the if expression)
- 06Repeating (the for expression)
- 07Writing your own function
- 08Repeating (the while loop)
- 09Working with lists (List)
- 10Working with maps (Map)
- 11Classes (object orientation)
- 12case class
- 13Inheritance and traits
- 14Pattern matching (the match expression)
- 15The Option type (null safety)
- 16Higher-order functions, map and filter
- 17object (singletons)
- 18Strings and string interpolation
- 19for comprehensions
- 20[Project] Build a small inventory system
Repeating (the for expression)
This lesson covers looping in Scala with a for expression.
You repeat with for (variable <- range). Using a range such as 1 to 5 is the typical form.
The sample code writes for (i <- 1 to 5) to print the numbers 1 through 5 in order.
A common early stumble is the difference between to (last number included) and until (last number excluded). 1 to 5 means 1–5 and 1 until 5 means 1–4, so check that you have the range you intended.
In professional work, for expressions are used to process every element of a list, but higher-order functions such as map — covered in a later lesson — are just as common.
๐งช This site can't compile or run Scala directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
