Share c++ looping source code

camthao421

New member
### C ++ mã nguồn lặp

#### 1. cho vòng lặp

Vòng lặp `for` là loại vòng lặp phổ biến nhất trong C ++.Nó lặp lại trong một loạt các giá trị, thực hiện một khối mã cho mỗi giá trị.Cú pháp của vòng `for` như sau:

`` `C ++
cho (khởi tạo; điều kiện; gia tăng) {
// Mã được thực thi cho mỗi giá trị
}
`` `

Câu lệnh `khởi tạo` được thực thi một lần trước khi vòng lặp bắt đầu.Câu lệnh `điều kiện` được đánh giá trước mỗi lần lặp của vòng lặp.Nếu điều kiện là đúng, khối mã bên trong vòng lặp được thực thi.Câu lệnh `gia tăng` được thực thi sau mỗi lần lặp của vòng lặp.

Dưới đây là một ví dụ về vòng lặp `for` in các số từ 1 đến 10:

`` `C ++
for (int i = 1; i <= 10; i ++) {
std :: cout << i << std :: endl;
}
`` `

#### 2. Trong khi vòng lặp

`While vòng lặp lặp qua một khối mã miễn là điều kiện là đúng.Cú pháp của vòng `while` như sau:

`` `C ++
while (điều kiện) {
// mã sẽ được thực thi trong khi điều kiện là đúng
}
`` `

Câu lệnh `điều kiện` được đánh giá trước mỗi lần lặp của vòng lặp.Nếu điều kiện là đúng, khối mã bên trong vòng lặp được thực thi.Nếu điều kiện là sai, vòng lặp chấm dứt.

Dưới đây là một ví dụ về vòng lặp `while in các số từ 1 đến 10:

`` `C ++
int i = 1;
while (i <= 10) {
std :: cout << i << std :: endl;
i ++;
}
`` `

#### 3. Làm trong khi vòng lặp

Vòng `DO while giống nhau tương tự như vòng lặp` while, ngoại trừ khối mã bên trong vòng lặp được thực thi ít nhất một lần, ngay cả khi điều kiện là sai.Cú pháp của vòng lặp `do` như sau:

`` `C ++
LÀM {
// mã sẽ được thực thi ít nhất một lần
} trong khi (điều kiện);
`` `

Câu lệnh `điều kiện` được đánh giá sau mỗi lần lặp của vòng lặp.Nếu điều kiện là đúng, khối mã bên trong vòng lặp được thực thi.Nếu điều kiện là sai, vòng lặp chấm dứt.

Dưới đây là một ví dụ về vòng lặp `DO while in các số từ 1 đến 10:

`` `C ++
int i = 1;
LÀM {
std :: cout << i << std :: endl;
i ++;
} while (i <= 10);
`` `

#### 4. Tuyên bố phá vỡ

Tuyên bố `break` có thể được sử dụng để chấm dứt một vòng lặp sớm.Câu lệnh `break` có thể được sử dụng trong bất kỳ loại vòng lặp.Cú pháp của câu lệnh `break` như sau:

`` `C ++
phá vỡ;
`` `

Dưới đây là một ví dụ về vòng lặp `for` chấm dứt khi giá trị của` i` bằng 5:

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

#### 5. Tuyên bố tiếp tục

Câu lệnh 'Tiếp tục' có thể được sử dụng để bỏ qua phần còn lại của khối mã bên trong một vòng lặp và tiếp tục với lần lặp tiếp theo của vòng lặp.Câu lệnh 'Tiếp tục' có thể được sử dụng trong bất kỳ loại vòng lặp.Cú pháp của câu lệnh 'Tiếp tục' như sau:

`` `C ++
Tiếp tục;
`` `

Dưới đây là một ví dụ về vòng lặp `for` in các số từ 1 đến 10, ngoại trừ số 5:

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

### hashtags

* #C ++
=======================================
### C++ Looping Source Code

#### 1. For Loop

The `for` loop is the most common type of loop in C++. It iterates over a range of values, performing a block of code for each value. The syntax of a `for` loop is as follows:

```c++
for (initialization; condition; increment) {
// code to be executed for each value
}
```

The `initialization` statement is executed once before the loop begins. The `condition` statement is evaluated before each iteration of the loop. If the condition is true, the code block inside the loop is executed. The `increment` statement is executed after each iteration of the loop.

Here is an example of a `for` loop that prints the numbers from 1 to 10:

```c++
for (int i = 1; i <= 10; i++) {
std::cout << i << std::endl;
}
```

#### 2. While Loop

The `while` loop iterates over a block of code as long as a condition is true. The syntax of a `while` loop is as follows:

```c++
while (condition) {
// code to be executed while condition is true
}
```

The `condition` statement is evaluated before each iteration of the loop. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop terminates.

Here is an example of a `while` loop that prints the numbers from 1 to 10:

```c++
int i = 1;
while (i <= 10) {
std::cout << i << std::endl;
i++;
}
```

#### 3. Do While Loop

The `do while` loop is similar to the `while` loop, except that the code block inside the loop is executed at least once, even if the condition is false. The syntax of a `do while` loop is as follows:

```c++
do {
// code to be executed at least once
} while (condition);
```

The `condition` statement is evaluated after each iteration of the loop. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop terminates.

Here is an example of a `do while` loop that prints the numbers from 1 to 10:

```c++
int i = 1;
do {
std::cout << i << std::endl;
i++;
} while (i <= 10);
```

#### 4. Break Statement

The `break` statement can be used to terminate a loop early. The `break` statement can be used in any type of loop. The syntax of the `break` statement is as follows:

```c++
break;
```

Here is an example of a `for` loop that terminates when the value of `i` is equal to 5:

```c++
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
std::cout << i << std::endl;
}
```

#### 5. Continue Statement

The `continue` statement can be used to skip the rest of the code block inside a loop and continue with the next iteration of the loop. The `continue` statement can be used in any type of loop. The syntax of the `continue` statement is as follows:

```c++
continue;
```

Here is an example of a `for` loop that prints the numbers from 1 to 10, except for the number 5:

```c++
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
std::cout << i << std::endl;
}
```

### Hashtags

* #C++
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top