Iterating through List with Scala for loop

This is a basic way to iterate through a list in a style not too different from Java’s.

It shows how for loops work in their simplest form.

object Runner extends App {
  
  val list1 = List(1,2,3,4)
  
  for (i <- list1) {
    print(s"i = $i. ")
  }
}

Output:

i = 1. i = 2. i = 3. i = 4. 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s