
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
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.