简体   繁体   中英

Using If-Statement as test condition for For-Loop in Vb.net

I've been writing some software in Vb.net and I've come to a point in the program where it would be best if I could place an if-statement in the header of a for-loop.

For example, in java, I could achieve what I need like so..

for (int I = 0; myArray[I].compareTo("") == 0; I ++)
{
    'code here
}

Unfortunately in Vb.net, all I know how to do is increment one number to a given number in a for-loop. I am aware however, that what I need to do can be accomplished using an if-test inside the for-loop

For I as Integer = 0 To myArray.length 'only possible test is comparison between two ints

    'code here
    If myArray(I).compareTo("") <> 0 Then
       Exit For
    End If

Next

Its not a big deal having to do this but if there is a way to streamline this more into the for-loop control then I would like to know for now and future reference.

So my question is, is it possible to check an if-condition (other than comparing two numbers) inside the header of a for-loop in Vb.net?

Update: In response to @Olivier Jacot-Descombes 's answer, I just wanted to clarify that I know while loops are used to test if-conditions in loops, but they lose the functionality of auto-incrementing possessed by for-loops. In Java, for-loops can do both of these. Which is why I'm wondering if Vb.net has the same functionality all within the header of a for-loop control somehow.

Use a While-Loop instead

Dim i As Integer = 0
While i < myArray.Length AndAlso String.IsNullOrEmpty(myArray(i))
    'Code here
    i += 1
End While

In VB a string can be empty ( "" ) or Nothing ( null in C#). In order to cope with both situations use String.IsNullOrEmpty(s) .

AndAlso (unlike And ) ensures shortcut evaluation. Ie if the first condition is not True then the second will not be evaluated. We need this here, otherwise the array would throw an "Index out of bounds" exception. Note also that the array index goes from 0 to array.Length - 1.

But you can also exit from a For-loop with Exit For

For I As Integer = 0 To myArray.Length-1

    'code here
    If Not String.IsNullOrEmpty(myArray(I)) Then
       Exit For
    End If

Next

But exiting a loop like this can make the code unreadable. The problem is that the For-loop has now 2 exit points and loop and exit conditions defined at different places.

There is also a Do...Loop statement allowing you to test the condition at the end of the loop.

The short answer is no. Visual Basic languages don't have anything like the C/java style for() loop.

The longer answer is that depending on what you want, you might not even need a loop.

Dim a = {"a", Nothing, "", "b"}

' this will print from 0 to 1, but Array.IndexOf returns -1 if value is not found
For i = 0 To Array.IndexOf(a, "") - 1
    Debug.Print(i & "")
Next

For Each item In a : If item = "" Then Exit For ' this is actually 2 lines separated by : 
    Debug.Print("'{0}'", item)
Next

For Each item In a.TakeWhile(Function(s) s > "") ' TakeWhile is a System.Linq extension
    Debug.Print("'{0}'", item)
Next

a.TakeWhile(Function(s) s > "").ToList.ForEach(AddressOf Debug.Print) ' prints a

a.TakeWhile(Function(s) s > "").ToList.ForEach(Sub(s) Debug.Print(s)) ' prints a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM