scala> for (c <- "1+1") println(c)
1
+
1
Is the same as
scala> "1+1" foreach (c => println(c))
1
+
1
Scalas for-comprehension is not a loop but a composition of higher order functions that operate on the input. The rules on how the for-comprehension is translated by the compiler can be read here.
Furthermore, in Scala Strings are collections. Internally they are just plain old Java Strings for which there exists some implicit conversions to give them the full power of the Scala collection library. But for users they can be seen as collections.
In your example foreach
iterates over each element of the string and executes a closure on them (which is the body of the for-comprehension or the argument in the foreach
call).
Even more information about for-comprehensions can be found in section 7 of the StackOverflow Scala Tutorial.