Scala while, do while and for loop (Syntax and example code)

If we talk about Scala control structures, Scala also has similar control structures as Java like while, do while and for loop. The concept is also same, so if we want write any iterative code than loops are very useful in any programming language.

Loops in Scala
Loops in Scala
While loop in Scala
  • While loop in Scala is same as while loop in Java
  • This loop is entry controlled loop, means loop condition is being checked before loop block is executed
  • Code block of this loop may or may not executed
Syntax
while(condition){ 
    statement(s)
}
While Loop Example
scala> var indexVar = 0
indexVar: Int = 0

scala> while(indexVar <= 10) {
     | println("Index Variable value : " + indexVar)
     | indexVar = indexVar + 1
     | }
Index Variable value : 0
Index Variable value : 1
Index Variable value : 2
Index Variable value : 3
Index Variable value : 4
Index Variable value : 5
Index Variable value : 6
Index Variable value : 7
Index Variable value : 8
Index Variable value : 9
Index Variable value : 10
Do While loop in Scala
  • Do While loop in Scala is also same as while loop in Java
  • This loop is exit controlled loop, means loop condition is being checked after loop block is executed
  • Code block of this is executed at least once
Syntax
do{ 
    statement(s)
} while(condition)
Do While Loop Example

After executing above let’s execute do while loop with same condition.

scala> do {
     | println("Index Variable value : " + indexVar)
     | indexVar = indexVar + 1
     | } while(indexVar <= 10)
Index Variable value : 11

It has executed once because condition is being checked after block execution.

Let’s change the condition and check the output.

scala> do {
     | println("Index Variable value : " + indexVar)
     | indexVar = indexVar + 1
     | } while(indexVar <= 20)
Index Variable value : 12
Index Variable value : 13
Index Variable value : 14
Index Variable value : 15
Index Variable value : 16
Index Variable value : 17
Index Variable value : 18
Index Variable value : 19
Index Variable value : 20

This is how do while works in Scala.

For loop in Scala

Syntax of for loop in Scala is quite different than Java. Let’s check it out.

Syntax
for (variable <- list) statement
//OR
for (variable <- range) statement
//OR
for (variable <- [range or list] if condition; if condition; ...) statement
For Loop Example
  • As per above syntaxes you can iterate through any list or range using for loop
  • You can also add one or more conditions to your iterations
  • Multiple Conditions will be added as AND conditions

Let’s have some examples,

//for (variable <- list) statement : Iterate through list
scala> var list = List(1,2,3,4,5,6,7,8,9,10)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> for(element <- list) println("Element of list is : " + element)
Element of list is : 1
Element of list is : 2
Element of list is : 3
Element of list is : 4
Element of list is : 5
Element of list is : 6
Element of list is : 7
Element of list is : 8
Element of list is : 9
Element of list is : 10

//for (variable <- range) statement : Iterate through Range 
scala> for(element <- 1 to 5) println("Element of range is : " + element)
Element of range is : 1
Element of range is : 2
Element of range is : 3
Element of range is : 4
Element of range is : 5

//for (variable <- [range or list] if condition; if condition; ...) statement
scala> for(element <- list if element%2 != 0; if element!= 7) println("Element of list is : " + element)
Element of list is : 1
Element of list is : 3
Element of list is : 5
Element of list is : 9

Stay tuned for more Scala related posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>