What is the Use of C++ Break Statement?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The break statement is a control statement that terminates the loop according to a particular condition. We can use the break statement using the break reserved keyword. The break statement is also used to avoid the infinite loop in the C++ program.

C++ Break Statement

The break statement in C++ terminates the loop in the particular C++ program. We use the break statement in C++ when we don't know the number of iterations it will take to complete a particular task. The break statement stops the iteration of the loop when the particular condition is fulfilled.

Syntax of C++ Break Statement

Let's understand the syntax of break statements in C++.

Syntax:

Example of C++ Break Statement

Let's understand the C++ break statement using an example.

In the below example, we are iterating on the unique elements of a given array to check whether the key is present in the array or not.

We use the break statement to terminate the loop when the key is found during the iteration because it will help to avoid further comparison in the array, which will increase the speed of execution of the C++ program and makes it efficient.

Output:

Uses of C++ Break Statement

Let's understand the use case of the C++ break statement.

  • The break statement avoids an infinite loop in the C++ program.
  • The break statement stops the execution of the iteration.
  • The break statement is used when we don't know the number of iterations used in a particular loop.

Working of C++ Break Statement

Let's understand the working of the C++ break statement with the help of the diagram. The break statement terminates the current execution of the loop.

Working of C++ Break Statement

Examples of C++ Break Statement

Let's understand the break statement in C++ using the below examples.

C++ Break with For Loop

Let's understand the use-case of the break statement in C++ using for loop.

In the example below, we are terminating the for loop when the number is divisible by 2 and 3.

Output:

C++ Break with While Loop

Let's understand the use-case of the break statement in C++ in the while loop.

In the below example, we are iterating on the infinite loop that is used to find the sum of all the digits of a given number, and it is terminated when the number becomes zero using the break statement.

Output:

C++ Break with Nested Loop

Let's understand the use of the break statement in the nested loop.

Nested Loop and C++ Break Statement with 2 Different Types of Loops

Nested Loops

Using an example, let's understand the use case of the break statement in the nested loop.

In the below example, we are printing all the pairs that don't have a start number as 2.

Output:

Infinite Loops

The break statement is also often used to avoid an infinite loop.

Conclusion

  • The break statement doesn't return any value.
  • The break statement doesn't take any value.
  • The break statement stops any iteration in the C++ program.
  • The break statement is mainly used to avoid an infinite loop.

FAQs

Q: Does the break statement return any value?

A: No, The break statement doesn't return any value.

Q: Does the break statement take any value ?

A: No, The break statement doesn't take any value.