Share vb.net while

lydiepviet.hung

New member
Vòng lặp ### vb.net trong khi vòng lặp

Một vòng lặp trong thời gian là một cấu trúc lập trình lặp lại một khối mã trong khi một điều kiện được chỉ định là đúng.Cú pháp trong một vòng lặp trong VB.NET như sau:

`` `
Dim i as integer = 0
Trong khi tôi <10
'Làm gì đó ở đây
i = i + 1
Kết thúc trong khi
`` `

Trong ví dụ này, vòng lặp sẽ lặp lại 10 lần, bắt đầu với giá trị của `I` là 0. Mỗi lần vòng lặp lặp lại, giá trị của` I` sẽ được tăng thêm 1.

Bạn cũng có thể sử dụng từ khóa `wend` để kết thúc vòng lặp trong thời gian.Mã sau đây cho thấy cách sử dụng từ khóa `wend`:

`` `
Dim i as integer = 0
Trong khi tôi <10
'Làm gì đó ở đây
i = i + 1
Wend
`` `

Từ khóa `Wend` không bắt buộc, nhưng nó có thể làm cho mã của bạn dễ đọc hơn.

### Ví dụ về các vòng lặp

Dưới đây là một số ví dụ về các vòng lặp trong vb.net:

* Để in các số từ 1 đến 10, bạn có thể sử dụng mã sau:

`` `
Dim I As Integer = 1
Trong khi tôi <= 10
Console.WriteLine (i)
i = i + 1
Kết thúc trong khi
`` `

* Để tổng hợp các số từ 1 đến 10, bạn có thể sử dụng mã sau:

`` `
Dim sum as integer = 0
Dim I As Integer = 1
Trong khi tôi <= 10
sum = sum + i
i = i + 1
Kết thúc trong khi
Console.WriteLine (tổng)
`` `

* Để kiểm tra xem một số là số nguyên tố, bạn có thể sử dụng mã sau:

`` `
Số dimer là số nguyên = 10
Dimer i as integer = 2
Dim isprime là boolean = true

Trong khi i <= math.sqrt (số)
Nếu số mod i = 0 thì
isprime = false
Kết thúc nếu
i = i + 1
Kết thúc trong khi

Nếu isprime thì
Console.WriteLine ("Số là số nguyên tố.")
Khác
Console.WriteLine ("Số không phải là nguyên tố.")
Kết thúc nếu
`` `

### Tài nguyên

* [Vb.net trong khi hướng dẫn vòng lặp] (https://www.tutorialspoint.com/vbnet/vbnet_while_loop.htm)
* [Vb.net trong khi các ví dụ vòng lặp] (Method in Global.asax file fires twice? - CodeProject example)
* [Vb.net trong khi tham chiếu vòng lặp] (https://docs.microsoft.com/en-us/do...ctions.specialized.stack?view=Net-6.0#methods)

### hashtags

* #vb.net
* #Programming
* #Loops
* #Conditionals
* #algorithms
=======================================
loop ### VB.NET While Loop

A while loop is a programming construct that repeats a block of code while a specified condition is true. The syntax for a while loop in VB.NET is as follows:

```
Dim i As Integer = 0
While i < 10
' Do something here
i = i + 1
End While
```

In this example, the loop will iterate 10 times, starting with the value of `i` being 0. Each time the loop iterates, the value of `i` will be incremented by 1.

You can also use the `Wend` keyword to end a while loop. The following code shows how to use the `Wend` keyword:

```
Dim i As Integer = 0
While i < 10
' Do something here
i = i + 1
Wend
```

The `Wend` keyword is not required, but it can make your code more readable.

### Examples of While Loops

Here are some examples of while loops in VB.NET:

* To print the numbers from 1 to 10, you could use the following code:

```
Dim i As Integer = 1
While i <= 10
Console.WriteLine(i)
i = i + 1
End While
```

* To sum the numbers from 1 to 10, you could use the following code:

```
Dim sum As Integer = 0
Dim i As Integer = 1
While i <= 10
sum = sum + i
i = i + 1
End While
Console.WriteLine(sum)
```

* To check if a number is prime, you could use the following code:

```
Dim number As Integer = 10
Dim i As Integer = 2
Dim isPrime As Boolean = True

While i <= Math.Sqrt(number)
If number Mod i = 0 Then
isPrime = False
End If
i = i + 1
End While

If isPrime Then
Console.WriteLine("The number is prime.")
Else
Console.WriteLine("The number is not prime.")
End If
```

### Resources

* [VB.NET While Loop Tutorial](https://www.tutorialspoint.com/vbnet/vbnet_while_loop.htm)
* [VB.NET While Loop Examples](https://www.codeproject.com/Articles/114049/VB-NET-While-Loop-Examples)
* [VB.NET While Loop Reference](https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stack?view=net-6.0#methods)

### Hashtags

* #vb.net
* #Programming
* #Loops
* #Conditionals
* #algorithms
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top