Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
C HAPTER 7 ยท Built-in Control Structures If there isn't a good justification for a particular while or do-while loop, try to find a way to do the same thing without it. 7.3 For expressions Scala's for expression is a Swiss army knife of iteration. It lets you combine a few simple ingredients in different ways to express a wide variety of itera- tions. Simple uses enable common tasks such as iterating through a sequence of integers. More advanced expressions can iterate over multiple collections of different kinds, can filter out elements based on arbitrary conditions, and can produce new collections. Iteration through collections The simplest thing you can do with for is to iterate through all the elements of a collection. For example, Listing 7.5 shows some code that prints out all files in the current directory. The I/O is performed using the Java API. First, we create a java.io.File on the current directory, "." , and call its listFiles method. This method returns an array of File objects, one per