Scala - Nested Loops



Nested loop means a loop statement inside another loop statement. That is why nested loops are also called loops inside loops. For example, nested loops, nested structures, nested conditional statements, etc.

Nested loops occur when a looping construct inside the body of another loop, termed as a nested loop or loops within a loop. The loop enclosing the other loop is called the outer loop. Whereas the enclosed one is the inner loop.

Syntax

The general form of a nested loop in Scala is -

Outer loop {
   Inner loop {
      ...
      ...
   }
   ... 
}

Scala provides three keywords for loop formation: while, do-while, and for. Nesting can be done on any of these loop types. For example, you can nest a while loop inside a for loop, a for loop inside a do-while loop, and any other combination. For each iteration of the outer loop, the inner loop completes all its iterations.

Nested For Loops

Nested for loops are common. If both the outer and inner loops are expected to perform three iterations each. The total number of iterations of the innermost statement will be "3 * 3 = 9".

Example

Try the following example program to understand nested for loop in Scala Programming Language:

object Demo {
   def main(args: Array[String]) {
      // outer loop
      for (i <- 1 to 3) {
         // inner loop
         for (j <- 1 to 3) {
            println(s"i: $i j: $j")
         }
         println("End of Inner Loop")
      }
      println("End of Outer Loop")
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
End of Inner Loop
i: 2 j: 1
i: 2 j: 2
i: 2 j: 3
End of Inner Loop
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3
End of Inner Loop
End of Outer Loop

In this example, for each iteration of the outer loop, the inner loop completes its iterations.

Nested Loops (while Loop Inside for Loop)

Any type of loop can be nested inside any other type. Let's rewrite the above example by putting a while loop inside the outer for loop.

Example

Try the following example program to understand Nested Loops (while Loop Inside for Loop) in Scala Programming Language:

object Demo {
   def main(args: Array[String]) = {
      // outer for loop
      for (i <- 1 to 3) {
         // inner while loop
         var j = 1
         while (j <= 3) {
            println(s"i: $i j: $j")
            j += 1
         }
         println("End of Inner While Loop")
      }
      println("End of Outer For Loop")
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
End of Inner While Loop
i: 2 j: 1
i: 2 j: 2
i: 2 j: 3
End of Inner While Loop
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3
End of Inner While Loop

Examples of Nested Loops

Printing Tables

The following program prints the tables of 1 to 10 with the help of two nested for loops.

object Demo {
   def main(args: Array[String]) = {
      println("Program to Print the Tables of 1 to 10")
      // outer loop
      for (i <- 1 to 10) {
         // inner loop
         for (j <- 1 to 10) {
            print("%4d".format(i * j))
         }
         println()
      }
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

Program to Print the Tables of 1 to 10
   1   2   3   4   5   6   7   8   9  10
   2   4   6   8  10  12  14  16  18  20
   3   6   9  12  15  18  21  24  27  30
   4   8  12  16  20  24  28  32  36  40
   5  10  15  20  25  30  35  40  45  50
   6  12  18  24  30  36  42  48  54  60
   7  14  21  28  35  42  49  56  63  70
   8  16  24  32  40  48  56  64  72  80
   9  18  27  36  45  54  63  72  81  90
  10  20  30  40  50  60  70  80  90 100

Printing Characters Pyramid

The following code prints an increasing number of characters from a string.

object Demo {
   def main(args: Array[String]) = {
      val x = "TutorialsPoint"
      val l = x.length
      // outer loop
      for (i <- 0 until l) {
         // inner loop
         for (j <- 0 to i) {
            print(x(j))
         }
         println()
      }
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

T
Tu
Tut
Tuto
Tutor
Tutori
Tutoria
Tutorial
Tutorials
TutorialsP
TutorialsPo
TutorialsPoi
TutorialsPoin
TutorialsPoint

Printing Two-Dimensional Array

This program displays a two-dimensional array of integers using nested loops.

object Demo {
   def main(args: Array[String]) = {
      val x = Array(Array(1, 2, 3, 4), Array(11, 22, 33, 44), Array(9, 99, 999, 9999), Array(10, 20, 30, 40))
      // outer loop
      for (i <- 0 to 3) {
         // inner loop
         for (j <- 0 to 3) {
            print("%5d".format(x(i)(j)))
         }
         println()
      }
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

1   2   3   4
11  22  33  44
9   99  999 9999
10  20  30  40

Note that there can be n-level nested loops, where n is any integer number. It depends on your program case.

Advertisements