Share vb.net while loop

heavylion959

New member
### 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ã miễn là một điều kiện được chỉ định là đúng.Cú pháp của vòng lặp trong thời gian trong vb.net như sau:

`` `
trong khi điều kiện
// Mã được thực thi
kết thúc trong khi
`` `

Ví dụ: mã sau in các số từ 1 đến 10:

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

Điều kiện trong một vòng lặp trong một thời gian có thể là bất kỳ biểu thức nào đánh giá theo giá trị boolean.Ví dụ: mã sau in các số từ 1 đến 10, nhưng bỏ qua số 5:

`` `
Dim I As Integer
i = 1
Trong khi tôi <= 10
Nếu tôi <> 5 thì
Console.WriteLine (i)
Kết thúc nếu
i = i + 1
kết thúc trong khi
`` `

Bạn cũng có thể sử dụng một vòng lặp thời gian để lặp lại một bộ sưu tập các mặt hàng.Ví dụ: mã sau in tên của tất cả các tệp trong một thư mục:

`` `
Thư mục Dim dưới dạng chuỗi = "C: \ TEMP"
Các tệp Dim dưới dạng FileSystemObject.DirectoryInfo = Directory.GetDirectories (Thư mục)
Đối với mỗi tệp dưới dạng FileSystemObject.DirectoryInfo trong các tệp
Console.WriteLine (File.Name)
Kế tiếp
`` `

### 5 hashtags

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

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

```
while condition
// code to be executed
end while
```

For example, the following code prints the numbers from 1 to 10:

```
Dim i As Integer
i = 1
while i <= 10
Console.WriteLine(i)
i = i + 1
end while
```

The condition in a while loop can be any expression that evaluates to a Boolean value. For example, the following code prints the numbers from 1 to 10, but skips the number 5:

```
Dim i As Integer
i = 1
while i <= 10
if i <> 5 Then
Console.WriteLine(i)
End If
i = i + 1
end while
```

You can also use a while loop to iterate over a collection of items. For example, the following code prints the names of all the files in a directory:

```
Dim directory As String = "C:\Temp"
Dim files As FileSystemObject.DirectoryInfo = Directory.GetDirectories(directory)
for Each file As FileSystemObject.DirectoryInfo In files
Console.WriteLine(file.Name)
next
```

### 5 Hashtags

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