Open In App

Swift – Break Statement

Improve
Improve
Like Article
Like
Save
Share
Report

The break statement is a loop control statement that is used to end the execution of an entire control flow statement immediately when it is encountered. When the break condition is true, the loop stops its iterations, and control returns immediately from the loop to the first statement after the loop. In simple words, break statement breaks the current flow of the program at specified conditions. The break statement can be used inside a loop or switch statement when you want to terminate the execution of the switch or loop statement earlier. This statement is generally used when we are not sure about the actual number of iterations we want or we want to terminate the loop based on some condition. 

Syntax:

break

Note: This statement always works with decision-making statements.

Flowchart: 

Break statement with Loops

Let us see the use of break statement in four different types of loops :

1. for-in loop: In Swift, for loop is an iterative statement used to check some conditions and then repeatedly execute a block of code for a certain number of time as long as those conditions are true. It can work with any sequence like an array, range, string, etc. The break statement is used to terminate the loop when the break condition is true. In the for-in loop, we put a break statement just below the break condition. 

Syntax :

for items in sequence{

    // block of code 
     break
}

Example: 

Swift




// Swift program illustrate the use of break
// statement with for-in loop
import Swift
 
print("Elements are:")
 
// For loop from 2 to 15
for val in 2...15 {
 
    // Condition for break statement
    if val == 11 {
        break
    }
     
    // Print statement
    print(val)
}


Output: 

Elements are:
2
3
4
5
6
7
8
9
10

Explanation: In the above example, we have used the for loop to print the value of val. For each time the condition for break statement is evaluated and when val is equal to 11, the break statement terminates the loop and exit out from the loop. Therefore the output will print the value up to 10 only. Suppose if there is no break condition in the above program then the output will print all values from 2 to 15. 

2. while Loop: Swift while loop is used to run a specific code until a certain condition is true. In the while loop, the break statement is used to terminate the while loop when the break condition is met.

Syntax:

while (condition)
{
    // statement
    
    // Condition for break statement
    if (condition) 
    {
        break
    }
}

Example:

Swift




// Swift program illustrate the use of break
// statement with while loop
import Swift
 
var i = 1
 
// While loop to find first 5 multiples of 2
while (i <= 10)
{
    // Print statement
    print("2 * \(i) = ", 2 * i)
     
    // condition for break statement
    if i >= 5 {
        break
    }
     
     
    i = i + 1
}


Output: 

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10

Explanation: In the above example, we used a while loop to print the first 5 multiples of 2. Notice the line, “if i >= 5 {break}”. This means when i is greater than or equal to 5, the while loop will terminate the loop and print the first 5 multiples of 2.

3. Nested loops: We can also use a break statement with nested loops to break the flow of the loop at the specified condition.

Syntax:

// Outer for loop
for value1 in sequence1
{
    // Inner for loop
    for value2 in sequence2
    {
        // Break condition
        if (condition) 
        {
            break
        }
    }
}

Example: 

Swift




// Swift program illustrate the use of break
// statement with nested loops
import Swift
 
// Nested for loop
// Outer for loop
for i in 1...3
{
 
    // Inner for loop
    for j in 1...3
    {
     
        // Using break statement
        // inside the inner loop 
        if i == 2
        {
            break
        }
         
        // Print statement
        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

Explanation: In the above example, we have two for-in loops now we use the break statement inside the inner for-in loop that is “if i == 2 {break}”. Here, when the value of i is 2, the break statement terminates the inner loop and the control flow of the program moves to the outer loop and continues. That’s why the value of i = 2 is not displayed in the output.

Break statement with a Switch statement

The switch statement is used to execute a block of code over multiple if-else-if statements while matching complex patterns. It contains multiple cases statements that are used to perform different actions according to different conditions. The execution takes from top to bottom and compares with each case value. If there is a match, the statement present inside that case is executed and the entire switch statement finishes its execution as soon as the first matching switch case is completed. If the match is not found for the case, it moves to the next case. The default keyword is a code that runs if there is no case is matched.

Syntax:

// Switch condition
switch (expression) 
{  
    case value1:  
    
    // Print statement 
    break
    
    case value2:
    
    // Print statement 
    break
    .
    .
    .
    
    default:
    
    // Print statement 
    break
}  

Example:

Swift




// Swift program illustrate the use of break
// statement with switch statement
import Swift
 
// Program to find the language
let language = 5
 
// Switch condition
switch language
{
 
  case 1:
   
    // Print statement 
    print("Java")
    break
         
  case 2:
   
    // Print statement
    print("Python")
    break
         
  case 3:
   
    // Print statement
    print("C#")
    break
         
  case 4:
   
    // Print statement
    print("c++")
    break
         
  case 5:
   
    // Print statement
    print("Swift")
    break
         
  default:
   
    Print statement
    print("Invalid choice")
    break
}


Output: 

Swift

Explanation: In the above example, the value of language is 5. Now, we compare the value of language with each of the cases present in the switch statement. Since the value gets matches with case 5, the statement (“Swift”) inside case  5 is executed, and then the break keyword stops the execution inside the switch block and stops the program.



Last Updated : 09 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads