in , ,

Swift break Statement

Swift break Statement
Swift break Statement

Swift break Statement: In this tutorial, you will learn about the break statement with the assistance of examples.

Swift break Statement

In Swift, the break statement inside any loop offers you an approach to break or end the execution of the loop containing it and moves the execution to the next statement following the loop. It is quite often used with if..else construct.


The break statement is used to terminate the loop promptly when it is experienced.

The syntax of the break statement is:

break

Swift for loop

Swift if…else statement

Swift while loop



Swift break statement with for loop

We can use the break statement with for loop to terminate the loop when certain condition is met. For instance,

for i in 1...5 {
  
  if i == 3 {
    break
  }
 
print(i)
}

Output

1
2

In the above example, we have used the for loop to print the worth of I. Notice the use of the break statement,

if i == 3 {
  break
}

Here, when I is equivalent to 3, the break statement terminates the loop. Thus, the output does exclude values after 2.

Note: The break statement is quite often used with decision-making statements.


break with while Loop

We can likewise terminate a while loop using the break statement. For instance,

// program to find first 5 multiples of 6

var i = 1

while (i<=10) {
  print("6 * \(i) =",6 * i)

    if i >= 5 {
      break
   }
 
  i = i + 1
}

Output

6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30

In the above example, we have used while loop to track down the initial 5 multiples of 6. Here notice the line,

if i >= 5 {
  break
}

This means when i greater than or equal to 5, the while loop is terminated.


Swift break statement with nested loops

At the point when we use the break statement with nested loops, it terminates the inner loop. For instance,

// outer for loop
for i in 1...3 {

  // inner for loop
  for j in 1...3 {

    if i == 2 {
      break
    }

    print("i = \(i), j = \(j)")
  }
}

Output

i = 1, j = 1                                                                                                                            
i = 1, j = 2                                                                                                                            
i = 1, j = 3                                                                                                                            
i = 3, j = 1                                                                                                                            
i = 3, j = 2                                                                                                                            
i = 3, j = 3

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

if i == 2 {
  break
}

Here, when the value in the event that I is 2, the break statement is executed. It ends the inner loop and the control flow of the program moves to the outer loop.

Subsequently, the value of I = 2 is never displayed in the output.


Swift Labeled break

Till now, we have used the unlabeled break statement. Be that as it may, there’s another type of break statement in Swift known as the labeled break.

When using nested loops, we can end the outer loop with a labeled break statement.

As you can see in the above picture, we have used the outerloop identifier to indicate the outer loop. Presently, notice how the break statement is used (break outerloop).

Here, the break statement is ending the labeled statement (for example outer loop). At that point, the control of the program leaps to the statement after the labeled statement.

Example: Labeled Statement with break

outerloop: for i in 1...3{

  innerloop: for j in 1...3 {

    if j == 3 {
      break outerloop
    }

      print("i = \(i), j = \(j)")
  }
}

Output

i = 1, j = 1
i = 1, j = 2

In the above example, we have labeled our loops as:

outerloop: for i in 1...3 {...}
innerloop: for j in 1...3 {...}

This assists with distinguishing the loops. Notice the use of labeled break statement.

if j == 3 {
  break outerloop
}

Here, the break statement will currently end the outer loop named with outerloop.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Nested Loops in Swift

Nested Loops in Swift

Swift Continue Statement

Swift Continue Statement