Share break for loop vb.net

vietquocbuffett

New member
** Cách phá vỡ vòng lặp cho VB.NET **

Đối với các vòng lặp là một cấu trúc lập trình phổ biến được sử dụng để lặp lại một bộ sưu tập các mục.Tuy nhiên, có thể có những lúc bạn cần thoát ra khỏi vòng lặp sớm, vì bạn đã đạt đến cuối bộ sưu tập hoặc do lỗi đã xảy ra.

Trong vb.net, có hai cách để phá vỡ một vòng lặp:

*** Sử dụng câu lệnh `break` **.Tuyên bố `break` ngay lập tức chấm dứt vòng lặp và tiếp tục thực hiện với câu lệnh theo vòng lặp.
*** Sử dụng câu lệnh `EXIT for` **.Câu lệnh `EXIT FOR` cũng chấm dứt vòng lặp, nhưng nó không bỏ qua các câu lệnh theo vòng lặp.

Dưới đây là một ví dụ về cách sử dụng câu lệnh `break` để thoát ra khỏi vòng lặp:

`` `VBNet
Số Dim As Integer () = {1, 2, 3, 4, 5}

Đối với tôi là số nguyên = 0 đến số.length - 1
Nếu số (i) = 3 thì
phá vỡ
Kết thúc nếu

Console.WriteLine (Số (i))
Kế tiếp
`` `

Mã này sẽ in các số 1, 2 và 4 vào bảng điều khiển.

Dưới đây là một ví dụ về cách sử dụng câu lệnh `exit for` để thoát ra khỏi vòng lặp:

`` `VBNet
Số Dim As Integer () = {1, 2, 3, 4, 5}

Đối với tôi là số nguyên = 0 đến số.length - 1
Nếu số (i) = 3 thì
Thoát khỏi cho
Kết thúc nếu

Console.WriteLine (Số (i))
Kế tiếp
`` `

Mã này sẽ in các số 1, 2 và 3 vào bảng điều khiển.

** Hashtags: **

* #vb.net
* #Cho vòng lặp
* #Break Vòng lặp
* #Programming
* #tutorial
=======================================
**How to Break a For Loop in VB.NET**

For loops are a common programming construct used to iterate over a collection of items. However, there may be times when you need to break out of a for loop early, either because you have reached the end of the collection or because an error has occurred.

In VB.NET, there are two ways to break a for loop:

* **Using the `break` statement**. The `break` statement immediately terminates the loop and continues execution with the statement following the loop.
* **Using the `Exit For` statement**. The `Exit For` statement also terminates the loop, but it does not skip the statements following the loop.

Here is an example of how to use the `break` statement to break out of a for loop:

```vbnet
Dim numbers As Integer() = {1, 2, 3, 4, 5}

For i As Integer = 0 To numbers.Length - 1
If numbers(i) = 3 Then
break
End If

Console.WriteLine(numbers(i))
Next
```

This code will print the numbers 1, 2, and 4 to the console.

Here is an example of how to use the `Exit For` statement to break out of a for loop:

```vbnet
Dim numbers As Integer() = {1, 2, 3, 4, 5}

For i As Integer = 0 To numbers.Length - 1
If numbers(i) = 3 Then
Exit For
End If

Console.WriteLine(numbers(i))
Next
```

This code will print the numbers 1, 2, and 3 to the console.

**Hashtags:**

* #vb.net
* #for loop
* #Break loop
* #Programming
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top