how loop in scala

for ((name, count) <- names.zipWithIndex) {
    println(s"$count is $name")
}

4.25
4
Shashwat 130 points

                                    res: Seq[(Int, Char)] = List((1,a), (1,b), (1,c), 
                             (2,a), (2,b), (2,c),
                             (3,a), (3,b), (3,c))

4.25 (4 Votes)
0
4.2
10
J.-E. Pin 100 points

                                    for (i &lt;- 1 to 3) println(i)

4.2 (10 Votes)
0
3.88
8
Yedrek 110 points

                                    names.zipWithIndex.foreach { d =&gt;
    println(s&quot;${d._2} is ${d._1}&quot;)
}

3.88 (8 Votes)
0
4.33
6
Phill Healey 130 points

                                    val ratings = Map(
    &quot;Lady in the Water&quot;-&gt; 3.0, 
    &quot;Snakes on a Plane&quot;-&gt; 4.0, 
    &quot;You, Me and Dupree&quot;-&gt; 3.5
)

for ((name,rating) &lt;- ratings) println(s&quot;Movie: $name, Rating: $rating&quot;)

4.33 (6 Votes)
0
0
0
John Smart 135 points

                                    val lengths = for (e &lt;- names) yield {
    // imagine that this required multiple lines of code
    e.length
}

0
0
3.67
6
Ddodsworthii 105 points

                                    for (i &lt;- 0 until names.length) {
    println(s&quot;$i is ${names(i)}&quot;)
}

3.67 (6 Votes)
0
3.75
4
Chria 90 points

                                    for (i &lt;- 1 to 10 if i &lt; 4) println(i)

3.75 (4 Votes)
0
3.86
7

                                    for {
    i &lt;- 1 to 10
    if i &lt; 4
} println(i)

3.86 (7 Votes)
0
4.29
7

                                    val names2 = for (e &lt;- names) yield e.capitalize

4.29 (7 Votes)
0
Are there any code examples left?
New code examples in category Scala
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source