您的位置:首页 > 编程语言 > VB

VB.Net学习笔记(循环语句)

2008-04-24 17:49 351 查看

循环语句

VB.Net中的循环语句分为:Do While Loop、For Next、For Each三种。

Do While Loop

Do While Loop有三种形式,这系列的循环是用于预先不知道循环的上限时使用的。在使用Do While Loop语句时要注意,因为它们是不确定循环次数,所以要小心不要造成死循环。 Do While Loop举例Public Class TestA Public Sub New() Dim i As Int32 i = 1 Do While i < 100 '先判断后执行 i += 1 Exit Do Loop i = 1 Do i += 1 Exit Do Loop While i < 100 '先执行后判断 While i < 100 'Do While i < 100 i += 1 Exit While End While End SubEnd Class

For Next

和Do While Loop不一样,For Next是界限循环。For 语句指定循环控制变量、下限、上限和可选的步长值。 For Next举例Public Class TestA Public Sub New() Dim i As Int32 For i = 0 To 100 Step 2 Next i End SubEnd Class

For Each

For Each也是不定量循环, For Each是对于集合中的每个元素进行遍历。如果你需要对一个对象集合进行遍历,那就应该使用For Each。 For Each举例Public Class TestA Public Sub New() Dim Found As Boolean = False Dim MyCollection As New Collection For Each MyObject As Object In MyCollection If MyObject.Text = "Hello" Then Found = True Exit For End If Next End SubEnd Class 简单的语句介绍,我们就到这里了,其他语句在以后对VB.Net的逐步深入中,我们会一一阐述。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: