Monday, 31 August 2020

Scala: for loop works for any collection

‘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



Previous                                                    Next                                                    Home

No comments:

Post a Comment