如:怎样让循环变量取0、1、3这三个值
解决方案 »
- 调用shell命令,如何知道外部程序已经完成操作?郁闷中...
- vb里面如果把combo设置成dropdownlist 如何取得选中的值
- 急,哪位大虾知道MSFlex Grid如何实现多行选中
- 关于共享打印的管理的东东
- 怎样实现在Event中参数为自定义结构?(在线)
- 情人节
- VB里DBGrid控件,怎样进行多行选择,然后删除?请各位兄弟帮忙?
- DCom问题:能不能在server中使用MS COMM控件?
- 请问怎样摸拟键盘上的上下左右等一些特殊的键?
- 如何将form的图标换成自己的icon图标。
- 为什么我在打包(PACKAGE)时老是出现错误
- 巨急,mshFlexGrid的数据显示完以后,怎样进行排序?在线等
for i=0 to 3
if i=2 then
i=i+1
endif
next i
'这里写正常代码
Debug.Print i
'……
If i = 1 Then i = 2
Next
for i=0 to 3
if i<>2 then
'代码
end if
next i
霸王和bulebeer的都有问题
i的数值被改变了,跳过了下也个数!!
Dim buff() As Long
ReDim buff(2)
buff(0) = 0
buff(1) = 1
buff(2) = 3
Dim i As Long
For i = 0 To 2
'下面用buff(i)替换循环体中的i
Next
Erase buff
End Sub
Dim s As String
s = "013"
For i = 0 To 1000
DoEvents
k = Val(Mid(s, (i Mod 3) + 1, 1))
Debug.Print k
Next
End Sub
.....
dim SearchString as String
SearchString="0,2,3"
for xx=StartValue to EndValue
if instr(SearchString,str(xx))>0 then
.....
end if
next
......
Dim i As Integer, j As Integer
For i = 0 To 100
j = 2 ^ (i Mod 3) - 1
Debug.Print j
Next
End Sub
if i<>2 then
此处加处理代码
endif
next i在FOR循环里并不能改变FOR的初始值。但可以在时间处理时过滤掉i=2时的事件。
这个办法是最正规的,因为可扩展啊。不用多说了,想一下就知道了。
if i<>2 then
此处加处理代码
endif
next i
不建议用类似的代码,原因是:假如我把问题:
//怎样让循环变量取0、1、3这三个值
修改为
//怎样让循环变量取0、29734、42111这三个值
那按照上面的方法,岂不是要执行4万多次?
Private Sub Command1_Click()
Dim buff() As Long
ReDim buff(2)
buff(0) = 0
buff(1) = 1
buff(2) = 3
Dim i
For Each i In buff()
Debug.Print i
Next
Erase buff
End Sub
Private Sub Command1_Click()
Dim j As Long
Static i As Long
Do
j = Switch(i = 0, 0, i = 1, 1, i = 2, 3)
Debug.Print j
i = i + 1
Loop While j < 3
End Sub
所以:
for i=1 to 3
'下面用F(i)替换循环体中的i
next i
for i=0 to 3
if i=0 or i=1 or i=3 then
endif
next i
Str = "013"
For i = 1 To Len(Str)
Debug.Print Mid(Str, i, 1)
Next
Dim a(1 To 3) As Integer, k As Integer
a(1) = 0: a(2) = 1: a(3) = 3
For Each k In a
Print k
Next k
10.9.1 While...End While and Do...Loop Statements | 10.9.2 For...Next Statements | 10.9 Loop Statements | For Each...Next Statements (Visual Basic Language Reference) | For Each...Next Statements (Visual Basic Language Concepts)
A For Each...Next statement loops based on the elements in an expression. A For Each statement specifies a loop control variable and an enumerator expression.The loop control variable is specified either through an identifier followed by an As clause or an expression. In the case of an identifier, the identifier defines a new local variable of the type specified in the As clause, scoped to the entire For Each loop. In the case of an expression, the expression must be classified as a variable. The enumerator expression must be classified as a value and its type must be a collection type. An implicit conversion must exist from the element type of the collection to the type of the loop control variable.The loop control variable cannot be used by another enclosing For Each statement. A For Each statement must be closed by a matching Next statement. A Next statement without a loop control variable matches the innermost open For Each. A Next statement with one or more loop control variables will, from left to right, match the For Each loops that have the same loop control variable. If a variable matches a For Each loop that is not the most nested loop at that point, a compile-time error occurs.A type C is said to be a collection type if it implements the interface System.Collections.IEnumerable or if all of the following are true: C contains an accessible instance method with the signature GetEnumerator() that returns a type E.
E contains an accessible instance method with the signature MoveNext() and the return type Boolean.
E contains an accessible instance property named Current that has a getter. The type of this property is said to be the element type of the collection type.
Following is an example of a class that can be enumerated:Public Class IntegerCollection
Private integers(10) As Integer Public Class IntegerCollectionEnumerator
Private collection As IntegerCollection
Private index As Integer = -1 Friend Sub New(ByVal c As IntegerCollection)
collection = c
End Sub Public Function MoveNext() As Boolean
index += 1 Return index <= 10
End Function Public ReadOnly Property Current As Integer
Get
If index < 0 OrElse index > 10 Then
Throw New System.InvalidOperationException()
End If Return integers(index)
End Get
End Property
End Class Public Sub New()
Dim i As Integer For i = 0 To 10
integers(i) = I
Next i
End Sub Public Function GetEnumerator() As IntegerCollectionEnumerator
Return New IntegerCollectionEnumerator(Me)
End Function
End Class
Before the loop begins, the enumerator expression is evaluated and cast to System.Collections.IEnumerable if it does not satisfy the requirements above. GetEnumerator is called on the resulting value and the return value of the function is stored in a temporary. Then at the beginning of each loop, MoveNext is called on the temporary. If it returns False, the loop terminates. If it returns True, the Current property is retrieved, coerced to the type of the iterator variable (regardless of whether the conversion is implicit or explicit), and assigned to the iterator variable; then the loop block executes. When the Next statement is reached, execution returns to the top of the loop. If a variable is specified after the Next keyword, it must be the same as the first variable after the For Each. For example, consider the following code:Imports SystemModule Test
Sub Main()
Dim i As Integer
Dim c As IntegerCollection = New IntegerCollection() For Each i In c
Console.WriteLine(i)
Next i
End Sub
End Module
It is equivalent to the following code:Imports SystemModule Test
Sub Main()
Dim i As Integer
Dim c As IntegerCollection = New IntegerCollection() Dim e As IntegerCollectionEnumerator e = c.GetEnumerator()
While e.MoveNext()
i = e.Current Console.WriteLine(i)
End While
End Sub
End Module
If the type E of the enumerator implements System.IDisposable, then the enumerator is disposed upon exiting the loop by calling the Dispose method. This ensures that resources held by the enumerator are released. If the method containing the For Each statement does not use unstructured error handling, then the For Each statement is wrapped in a Try statement with the Dispose method called in the Finally to ensure cleanup.Note The System.Array type is a collection type, and since all array types derive from System.Array, any array type expression is permitted in a For Each statement. For single-dimensional arrays, the For Each statement enumerates the array elements in increasing index order, starting with index 0 and ending with index Length - 1. For multidimensional arrays, the indices of the rightmost dimension are increased first.
For example, the following code prints 1 2 3 4:Imports SystemModule Test
Sub Main()
Dim x(,) As Integer = { { 1, 2 }, { 3, 4 } }
Dim i As Integer For Each i In x
Console.Write(i & " ")
Next i
End Sub
End Module
It is not valid to branch into a For Each statement block from outside the block.ForEachStatement ::=
For Each LoopControlVariable In Expression StatementTerminator
[ Block ]
Next [Expression ] StatementTerminatorcopy from MSDN Library