‘for’ loop works for any collection.
Example 1: Traverse a string using for loop.
scala> for (data <- "Hello World"){
| println(data)
| }
H
e
l
l
o
W
o
r
l
d
Example 2: Travers a List using for loop.
scala> for (data <- List(2, 3, 5, 7)){
| println(data)
| }
2
3
5
7
No comments:
Post a Comment