3 ways of for-loop reverse in Scala

 370 total views,  1 views today

Welcome to Scala series. In this tutorial, you will learn 3 ways of for-loop reverse in Scala. There are 3 ways to iterate a range or a collection in reverse order. Let us look one by one with an example.

Using ‘to’ and ‘by’ keyword

for(i <- 10 to 1 by -1){
     println(i)
}

Using only ‘to’ keyword

for(i <- 10 to (1,-1)){
      println(i)
}

Using only reverse keyword

for(i <- 10 to 1 reverse){
      println(i)
}

These are the different ways of iterating a range or a collection in reverse order.

To learn more about for loop in Scala please refer For loop Scala

Hope this post was useful. Please leave a comment and give us a thumbs up.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.