Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

Swift - Control Statements in Loops

In Swift, loops allow you to run a block of code repeatedly. Control statements, on the other hand, let you influence or change the default behavior of these loops. Common control statements within loops include break, continue, and labeled statements. Let's delve into each of these:

1. break:

The break statement is used to terminate the loop immediately, regardless of whether its conditions were met.

for i in 1...10 {
    if i == 5 {
        break
    }
    print(i)  // Prints numbers 1 to 4
}

In this example, the loop terminates when i reaches 5.

2. continue:

The continue statement skips the remainder of the current iteration and jumps to the start of the next iteration.

for i in 1...10 {
    if i == 5 {
        continue
    }
    print(i)  // Prints numbers 1 to 4 and 6 to 10, skipping 5
}

Here, when i is 5, the loop skips the print statement and moves directly to the next iteration.

3. Labeled Statements:

Swift provides a unique feature known as labeled statements. You can label a loop (or even a conditional statement), and then use the break or continue statement with that label, allowing you to specify which loop or condition you want to break out of or continue.

outerLoop: for i in 1...3 {
    innerLoop: for j in 1...3 {
        if j == 2 {
            continue outerLoop
        }
        print("i: \(i), j: \(j)")
    }
}

In the above example, the continue outerLoop statement will skip the rest of the current iteration of the outer loop, not just the inner one, when j equals 2.

4. while and repeat-while Loops:

Both while and repeat-while loops can also utilize control statements.

  • while:

    var i = 1
    while i <= 10 {
        if i == 5 {
            break
        }
        print(i)
        i += 1
    }
    
  • repeat-while (similar to do-while in other languages):

    var i = 1
    repeat {
        if i == 5 {
            break
        }
        print(i)
        i += 1
    } while i <= 10
    

Conclusion:

Control statements in loops are integral for adding flexibility and conditional logic within repetitive tasks. They help manage the flow of a program and allow developers to cater to specific conditions effectively. Swift's addition of labeled statements further enhances the control over nested loops.

  1. Using break and continue in Swift loops:

    • Description: The break statement is used to exit a loop prematurely, while continue is used to skip the rest of the code inside the loop for the current iteration.

    • Code:

      for i in 1...5 {
          if i == 3 {
              continue // Skip iteration when i is 3
          }
          print(i)
      
          if i == 4 {
              break // Exit the loop when i is 4
          }
      }
      
  2. Conditional statements inside Swift loops:

    • Description: Swift loops can contain conditional statements (if and else) to control the flow of execution within the loop.

    • Code:

      for number in 1...10 {
          if number % 2 == 0 {
              print("\(number) is even")
          } else {
              print("\(number) is odd")
          }
      }
      
  3. Loop control flow in Swift programming:

    • Description: Loop control flow refers to the ability to control the order and conditions under which the loop executes its code.

    • Code:

      var sum = 0
      var count = 0
      
      while true {
          sum += count
          count += 1
      
          if count > 5 {
              break // Exit the loop when count is greater than 5
          }
      }
      print("Sum: \(sum)")
      
  4. Swift for loop with break and continue:

    • Description: The for loop in Swift can also use break and continue statements to control its execution.

    • Code:

      for i in 1...5 {
          if i == 3 {
              continue // Skip iteration when i is 3
          }
          print(i)
      
          if i == 4 {
              break // Exit the loop when i is 4
          }
      }
      
  5. Nested loops and control statements in Swift:

    • Description: Swift supports nested loops, and control statements can be used to control the flow within these nested structures.

    • Code:

      outerLoop: for i in 1...3 {
          innerLoop: for j in 1...3 {
              print("(\(i), \(j))")
      
              if j == 2 {
                  break outerLoop // Exit both loops when j is 2
              }
          }
      }
      
  6. How to skip iterations in Swift loops:

    • Description: continue statement is used to skip the rest of the code in the loop and move to the next iteration.

    • Code:

      for i in 1...5 {
          if i % 2 == 0 {
              continue // Skip even numbers
          }
          print(i)
      }
      
  7. Using if-else in Swift loop conditions:

    • Description: Swift loop conditions can include if-else statements to make decisions based on certain conditions.

    • Code:

      for i in 1...10 {
          if i % 2 == 0 {
              print("\(i) is even")
          } else {
              print("\(i) is odd")
          }
      }
      
  8. Swift while loop control statements:

    • Description: Similar to for loops, while loops can use break and continue to control their execution.

    • Code:

      var count = 0
      
      while count < 5 {
          print(count)
      
          if count == 3 {
              break // Exit the loop when count is 3
          }
      
          count += 1
      }
      
  9. Loop labels and control in Swift programming:

    • Description: Loop labels can be used with break and continue statements to specify which loop should be affected.

    • Code:

      outerLoop: for i in 1...3 {
          innerLoop: for j in 1...3 {
              print("(\(i), \(j))")
      
              if j == 2 {
                  break outerLoop // Exit the outer loop when j is 2
              }
          }
      }