How Do You Make a Loop Iterate Early Without Using GoTo?

  • Thread starter Thread starter eBob.com
  • Start date Start date
E

eBob.com

How do you make a loop iterate early without using a GoTo? (I guess
I've done too much structured programming and I really don't like
using GoTos.)

Here's my code ...

For Each Thing As OFI In FileInfo
If Thing.Displayed <> True Then
GoTo Iterate 'skip this entry; try next one
End If

If yada yada yada Then
TextBox2.Text = Thing.Name
End If
Iterate:
Next Thing

I'm thinking that there has to be some way to do this without using
GoTo but I've not been able to find it.

Thanks, Bob
 
Hi,

What is wrong with this.


For Each Thing As OFI In FileInfo
If Thing.Displayed = True Then
If yada yada yada Then
TextBox2.Text = Thing.Name
End If
End If
Next Thing


Ken
-----------------------
How do you make a loop iterate early without using a GoTo? (I guess
I've done too much structured programming and I really don't like
using GoTos.)

Here's my code ...

For Each Thing As OFI In FileInfo
If Thing.Displayed <> True Then
GoTo Iterate 'skip this entry; try next one
End If

If yada yada yada Then
TextBox2.Text = Thing.Name
End If
Iterate:
Next Thing

I'm thinking that there has to be some way to do this without using
GoTo but I've not been able to find it.

Thanks, Bob
 
Thanks Ken. Yes. I meant to say in my post that I knew I could avoid
the GoTo by restructuring the If. And I suppose that even works in
general. BUT ... I am still wondering if there is some language
construct specifically for this. I also meant to say that I was
looking for the equivalent of the C/C++ continue statement.

But you would have mentioned the VB equivalent of the the C/C++
continue statement in your response, so I am gathering that VB does
not have such a construct. I guess I can live without it.

Thanks again, Bob
 
Any number of Exit For statements may be placed anywhere in the loop as an
alternative way to exit. Exit For is often used after evaluating some
condition, for example with an If...Then...Else statement, and transfers
control to the statement immediately following Next.

-L
 
What he is asking for is a way to skip to the end of the for-next loop
and immediately go to the next item, not exit the loop.
 
Bob,

Good structuring your program is not only avoiding the Goto however as well
some other commands where it was by instance in Cobol the Alter which was
one of the options to continue on a different place again.

A lot of us think they should be banned from program languages.

Programmers who made non structured programs will always exist and therefore
these awfull commands are comming back in new languages where they first
where banned because those programmers are screaming for them because they
had them in past.

I would go for the code from Ken.

And when you cannot, look first for another classic awfull command, however
what is implemented in VBNet very good for structuring your program; the
Select. Case

Just my thought,

Cor
 
Back
Top