Skip to content

Commit

Permalink
#108 break statement content is added (#146)
Browse files Browse the repository at this point in the history
* break statement content is added

* removed
  • Loading branch information
gangwarsomya authored Jun 6, 2024
1 parent 5faafad commit 5203a61
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions docs/day-06/break-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,67 @@ sidebar_label: "Break Statement"
slug: break-statement-in-cpp
---

TASK:
## What is the Break Statement in C++?
The break statement in C++ is a control statement used to exit prematurely from loops (like for, while, or do-while loops) and switch statements. When encountered, the break statement immediately terminates the loop it's contained within, regardless of the loop's normal exit conditions. This allows for an early exit based on certain conditions without executing the remaining code inside the loop.




![Constants in CPP](../../static/img/day-04/constants-in-cpp.png)

## Explain the Syntax of Break Statement in C++.
The syntax of the break statement in C++ is simple and straightforward.It's used without any conditions or expressions. Here's the syntax:

### Syntax of break statement:

```cpp
break;

```
When encountered within a loop or a switch statement, the break statement immediately terminates the nearest enclosing loop or switch statement and transfers control to the statement immediately following the terminated loop or switch.

## C++ Break Statement Example

```cpp
#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
if (i == 3) {
cout << "Breaking out of the loop when i is 3." << endl;
break;
}
cout << i << " ";
}
return 0;
}

```

## C++ Break Statement in Nested Loop.
The break statement can be especially helpful in C++ when utilising nested loops to manage the execution flow. It lets you break out of nested loops at any number of levels under specific circumstances. The following is an example of how to use break in nested loops:

### Example

```cpp
#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 3; ++i) {
cout << "Outer loop iteration: " << i << endl;
for (int j = 1; j <= 3; ++j) {
cout << "Inner loop iteration: " << j << endl;
if (j == 2) {
cout << "Breaking out of the inner loop." << endl;
break; // Exits the inner loop when j equals 2
}
}
}
return 0;
}

```

1. What is the Break Statement in C++?
2. Explain the Syntax of Break Statement in C++.
3. C++ Break Statement Example
4. C++ Break Statement in Nested Loop

IMAGE FILE:
![Break Statement](../../static/img/day-06/break-statement.png)

0 comments on commit 5203a61

Please sign in to comment.