Share Break C++: Sử Dụng Câu Lệnh Break Trong Ngôn Ngữ Lập Trình C++

nguyenthai.duy

New member
## Break C ++

Câu lệnh Break trong C ++ được sử dụng để chấm dứt lần lặp vòng lặp hiện tại và tiếp tục thực thi tại câu lệnh tiếp theo theo vòng lặp.

Cú pháp của câu lệnh break như sau:

`` `
phá vỡ;
`` `

Câu lệnh Break có thể được sử dụng trong bất kỳ loại vòng lặp, bao gồm cả các vòng lặp, trong khi các vòng lặp và các vòng lặp trong khi.

Ví dụ: mã sau sử dụng vòng lặp For để in các số từ 1 đến 10. Câu lệnh Break được sử dụng để chấm dứt vòng lặp sau khi số 5 đã được in:

`` `
#include <Istream>

sử dụng không gian tên STD;

int main () {
for (int i = 1; i <= 10; i ++) {
if (i == 5) {
phá vỡ;
}
cout << i << endl;
}

trả lại 0;
}
`` `

Đầu ra:

`` `
1
2
3
4
5
`` `

Tuyên bố Break cũng có thể được sử dụng với câu lệnh tiếp tục để bỏ qua các câu lệnh còn lại trong lần lặp vòng lặp hiện tại.Ví dụ: mã sau sử dụng vòng lặp For để in các số từ 1 đến 10. Câu lệnh tiếp tục được sử dụng để bỏ qua số 5 khi gặp phải:

`` `
#include <Istream>

sử dụng không gian tên STD;

int main () {
for (int i = 1; i <= 10; i ++) {
if (i == 5) {
Tiếp tục;
}
cout << i << endl;
}

trả lại 0;
}
`` `

Đầu ra:

`` `
1
2
3
4
6
7
số 8
9
10
`` `

## hashtags

* #C ++
* #Programming
* #Loops
* Câu lệnh #Control
* #Breakbreak Statement
=======================================
## Break C++

The break statement in C++ is used to terminate the current loop iteration and resume execution at the next statement following the loop.

The syntax of the break statement is as follows:

```
break;
```

The break statement can be used in any type of loop, including for loops, while loops, and do-while loops.

For example, the following code uses a for loop to print the numbers from 1 to 10. The break statement is used to terminate the loop after the number 5 has been printed:

```
#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
cout << i << endl;
}

return 0;
}
```

Output:

```
1
2
3
4
5
```

The break statement can also be used with the continue statement to skip the remaining statements in the current loop iteration. For example, the following code uses a for loop to print the numbers from 1 to 10. The continue statement is used to skip the number 5 when it is encountered:

```
#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
cout << i << endl;
}

return 0;
}
```

Output:

```
1
2
3
4
6
7
8
9
10
```

## Hashtags

* #C++
* #Programming
* #Loops
* #Control Statements
* #Break Statement
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top