Nested Loops in Swift

If a loop exists inside the body of another loop, it's called a nested loop. Here's an example of the nested for loop.

// outer loop
for i in 1...5 {
  // codes

  // inner loop
  for j in 1...2 {
    //codes
  }
}

Here, the inner for loop for j in 1...2 is nested inside the outer for loop for i in 1...5.


Swift Nested for Loop

A nested for loop includes one for loop inside another for loop. For example,

// Swift program to display 7 days of 2 weeks

// outer loop
for week in 1...2 {
  print("Week: \(week)")

// inner loop
    for day in 1...7 {
      print("  Day:  \(day)")
  
   }

// line break after iteration of outer loop
   print("")
}

Output

Week: 1
  Day:  1
  Day:  2
...

Week: 2
  Day:  1
  Day:  2
...

In the above example, the outer loop iterates 2 times and prints 2 weeks: Week: 1 and Week: 2.

And, the inner loop iterates 7 times and prints the 7 days: Day: 1, Day: 2, and so on.

Note: Similarly, we can also create nested while and repeat...while loops. For example,

// outer while loop
while (condition) {
  ...

  // inner while loop
  while (condition2) {
    ...
  }
}

Swift for Loop inside a while Loop

We can also create nested loops of different types. That is, we can put a for loop inside a while loop and vice versa. For example,

// program to display 7 days of 2 weeks
var weeks = 2
var i = 1

// outer while loop
while (i <= weeks){
  print("Week: \(i)")

  // inner for loop
  for day in 1...7{
      print("  Day:  \(day)")
    }

    i = i + 1
}

Output

Week: 1
  Day:  1
  Day:  2
…

Week: 2
  Day:  1
  Day:  2
 ...

Here, we have used a for loop inside the while loop.

Note: Similarly, we can also nest a repeat...while loop inside a while or for loop.


break and continue Inside Nested Loop

1. break inside Nested Loop

When we use a break statement inside the inner loop, it terminates the inner loop but not the outer loop. For example,

// outer loop
for week in 1...3 {
  print("Week: \(week)")

  // inner loop
  for day in 1...7 {


    if(week == 2) {
      // use of break statement
      break
      }

    print("  Day:  \(day)")
   }

  print("")
}

Output

Week: 1
  Day:  1
  Day:  2
...

Week: 2

Week: 3
  Day:  1
  Day:  2
...

In the above example, we have used the break statement inside the inner for loop. Notice the line,

if(week == 2) {
  break
}

Here, the program terminates the loop when week is 2.

Hence, days for week 2 are not printed. However, the outer loop that prints week is unaffected.


2. continue inside a Nested Loop

Similarly, when we use a continue statement inside the inner loop, it skips the current iteration of the inner loop only. For example,

// outer loop
for week in 1...2 {
  print("Week: \(week)")

  // inner loop
  for day in 1...7 {

    // use of continue statement
    if(day % 2 != 0) {
      continue
      }

   print("  Day:  \(day)")
   }

  print("")
}

Output

Week: 1
  Day:  2
  Day:  4
  Day:  6

Week: 2
  Day:  2
  Day:  4
  Day:  6

In the above example, we have used the continue statement inside the inner for loop. Notice the code,

if(day % 2 != 0) {
  continue
}

Here, the continue statement is executed when the value of day is odd. Hence, the program only prints those days that are even.

We can see the continue statement has affected only the inner loop. The outer loop is working without any problem.

Did you find this article helpful?