请问怎样让for的循环变量取几个固定的值???

xsnowboy 2004-09-05 05:22:41
如:怎样让循环变量取0、1、3这三个值
...全文
567 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuwuyu 2004-09-07
  • 打赏
  • 举报
回复
刚才忘记把代码附上来了,很简单的!呵呵

Dim Str As String
Str = "013"
For i = 1 To Len(Str)
Debug.Print Mid(Str, i, 1)
Next
xuwuyu 2004-09-07
  • 打赏
  • 举报
回复
我来提供一个思路,我感觉比较通用。把循环条件放到字符串里,通过每次循环取一个字符,然后调用。呵呵

zzzzzz7 2004-09-07
  • 打赏
  • 举报
回复
有意思,学习
hongbo163 2004-09-07
  • 打赏
  • 举报
回复
呵呵,简单的一个问题
学到不少东西哦
ryuginka 2004-09-07
  • 打赏
  • 举报
回复
最笨的方法:

Dim i as integer
for i=0 to 3
if i=0 or i=1 or i=3 then

endif
next i
jeff__lueny 2004-09-07
  • 打赏
  • 举报
回复
Visual Basic Language Specification

10.9.3 For Each...Next StatementsSee Also
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 System

Module 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 System

Module 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 System

Module 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 ] StatementTerminator

copy from MSDN Library
jeff__lueny 2004-09-07
  • 打赏
  • 举报
回复
flyoutmouse(笨笨)的方法好啊,我第一次见,惭愧~~~
flyoutmouse 2004-09-07
  • 打赏
  • 举报
回复
奇怪的问题,如果循环变量的值要按自已的要求取值,何不用For each?如:
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
jordi2014 2004-09-07
  • 打赏
  • 举报
回复
rainstormmaster(暴风雨 v2.0) ( ) 的方法不错!
uitoo 2004-09-06
  • 打赏
  • 举报
回复
0、1、3 或 0、29734、42111这三个值都为数列,必能写成通项公式 An = F(n)
所以:
for i=1 to 3
'下面用F(i)替换循环体中的i
next i
VBDN 2004-09-06
  • 打赏
  • 举报
回复
实现的方法好多,来一个DO+Switch看看!
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
VBDN 2004-09-06
  • 打赏
  • 举报
回复
对暴风雨的代码做了修改,呵呵!
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
rainstormmaster 2004-09-05
  • 打赏
  • 举报
回复
//for i=0 to 3
if i<>2 then
此处加处理代码
endif
next i


不建议用类似的代码,原因是:假如我把问题:
//怎样让循环变量取0、1、3这三个值
修改为
//怎样让循环变量取0、29734、42111这三个值
那按照上面的方法,岂不是要执行4万多次?
  • 打赏
  • 举报
回复
唉,让rainstormmaster(暴风雨 v2.0)说了。
这个办法是最正规的,因为可扩展啊。不用多说了,想一下就知道了。
DemonLoveLizzy 2004-09-05
  • 打赏
  • 举报
回复
for i=0 to 3
if i<>2 then
此处加处理代码
endif
next i

在FOR循环里并不能改变FOR的初始值。但可以在时间处理时过滤掉i=2时的事件。
northwolves 2004-09-05
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim i As Integer, j As Integer
For i = 0 To 100
j = 2 ^ (i Mod 3) - 1
Debug.Print j
Next
End Sub
iiboy 2004-09-05
  • 打赏
  • 举报
回复
要不这样吧:
.....
dim SearchString as String
SearchString="0,2,3"
for xx=StartValue to EndValue
if instr(SearchString,str(xx))>0 then
.....
end if
next
......
iiboy 2004-09-05
  • 打赏
  • 举报
回复
你给的数列数据太少,得不出什么规律
lsftest 2004-09-05
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
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
BlueBeer 2004-09-05
  • 打赏
  • 举报
回复
to mccxj(爱吃饺子) ,你的办法也可以,但是你的循环被执行的次数仍然是4次,有一次空循环而已,不可取

rainstormmaster(暴风雨 v2.0)的才具有通用性,是个好主意
加载更多回复(5)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧