For ... Next ...

  • Thread starter Thread starter Ale K.
  • Start date Start date
A

Ale K.

i know that For...Next as a Exit For....
there is any way from the middle of my for...next code to go to the next
item and jump out part of my code , like the same thing that can be done
with exit for, but instead for go to the next item??

Thanks.
 
Hi, There is no built-in keyword to do this (There is in C#, it's continue).
You may need to have a large "If" statement. DO NOT USE GoTo's.

Try the following:

For Foo = Bar To Baz

Do
...
Loop Until True

Next

The ... is for your code, and when you want to skip your code, and go to the
next item, call 'Exit Do'. The Do...Loop will only run once.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Tom:

I know I'm going to start WWIII with this, but although GoTo should be
avoided, it definitely has its place. In VB.NET for instance, it's entirely
appropriate to use it to break out of a loop. If you use it to solve a
specific problem with one loop, and you simply use it to break out of a
loop, what is the real problem with this? And is writing convoluted IF
logic really more readable, faster, or safer?

Similarly, look at C# where there is no Fall through on a Switch Statement,
using goto to hit a specific case statement is entirely appropriate and even
desireable in many instances. Inside C# by Archer and WhiteChapel makes a
very convincing argument in favor of Goto as long as it's used correctly.
http://www.amazon.com/exec/obidos/t...f=sr_1_5/104-0968507-3087124?v=glance&s=books

Bill
 
I know I'm going to start WWIII with this, but although GoTo should be
avoided, it definitely has its place. In VB.NET for instance, it's entirely

Fortunately for those who hate GoTo, the next version of VB (Whidbey) has a
Continue For statement for just that very purpose.
 
I know I'm going to start WWIII with this...

Yes. Yes you are. ;-)
--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Hi, good good. I'd heard rumours of this.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
William Ryan said:
I know I'm going to start WWIII with this, but although GoTo should be
avoided, it definitely has its place.

William... I think you will find that the admonition about avoiding goto
statements was mostly due to it's being abused. It was used to jump to
something as arbitrary as a "line number." GOTO <label> isn't anywhere near
as meaningless as GOTO 11920 which is how earlier version of BASIC did it
when there were no labels.

The second reason was that they were used to jump huge distances and not to
escape out of a short loop in a routine that fits comfortably on a page in
the editor. "Self-evident" as a concept was lost when code jumped 1000+
lines to some arbitrary numbered spot. Once you got to line 11920 you'd
have to ask yourself "what variables are visible to me now ... and what
values might they have?"

You didn't even gave to GOTO the beginning of the subroutine, you could GOTO
anywhere... every line had a number.
 
Agreed, and as such, then that's one more reason not to use GoTo. The fact
that there are appropriate uses is based on the current language spec, that
could easily be corrected. Other than loops, the only other instance I can
think of is Fall Through in switch statements.
 
TOTALLY in Agreement with you. I'm not advocating it's use...I just think
that there are two instances where it's appropriate. I personally don't use
it except to break out of loops if I have to write in depth If logic and in
Switch statements to simulate fall through.
 
Hi Ale,

Since no-one's shown it yet...

For I = 1 To 52
DoSomethingAmazing (I)
If I > 26 Then _
Goto MissedTheBoringBit
DoSomethingBoring (I)

MissedTheBoringBit:
Next

Notice that there's a colon after the MissedTheBoringBit label.

Regards,
Fergus

Just a thought - GoTo is very useful to make up for certain deficiencies
in VB syntax. If anyone makes spaghetti these days I would be surprised. But
if they do, they are also doing far worse things in other areas of the code
and are probably beyond remedy anyway.
 
Fergus:

Excellent example...This is effectively what I was referring to with the
lack of a fall through in a switch/case statement. I don't like GoTo and
have seen it abused ad naseum....and although I totally agree with what you
said, trust me...there are more than a few Spaghetti Chefs out there serving
up Goto DuJour Pasta.

That's the whole problem with stuff like this. GoTo Sucks by and large and
is totally abused. however, everything, EVERYTHING, even <I can't believe
I'm actually saying this> late binding. In the hands of someone who knows
what they are doing and isn't cutting corners, it can bail you out of rare
problem areas. however, too many people like the shortcuts. GoTo is one of
those blindly partisan things...in my CS classes, you'd automatically fail
if you used GoTo (although there isn't any reason I've found in C++ to use
one). But languages have limitations, and the bottom line is coming up with
the best possible solution.

Always love reading your posts because you get the point across in a manner
that's as elegant as it is convincing. your example below is a prime
example ;-)

Cheers,

bill
 
Tom said:
The second reason was that they were used to jump huge distances and
not to escape out of a short loop in a routine that fits comfortably
on a page in the editor. "Self-evident" as a concept was lost when
code jumped 1000+ lines to some arbitrary numbered spot. Once you
got to line 11920 you'd have to ask yourself "what variables are
visible to me now ... and what values might they have?"

ALL the same variables were visible on line 11920 as line 550 in code I
worked on with my "screamer" 8 MHz 8086. These guys were too good to need
separately compiled modules. ;-) Predictably, the company went out of
business because of software reliability issues.

-- Mark
 
Hi Bill,

|| ... Spaghetti Chefs out there serving up Goto DuJour Pasta.

ROFL.

|| Always love reading your posts .. [more lovely things]

Blush.. Thank you kindly, sir. :-))

Cheers,
Fergus
 
Mark Jerde said:
ALL the same variables were visible on line 11920 as line 550 in code I
worked on with my "screamer" 8 MHz 8086. These guys were too good to need
separately compiled modules. ;-) Predictably, the company went out of
business because of software reliability issues.

Hi Mark,

Adding to the chaos BASIC didn't require the explicit declaration of
variables anywhere. And the routine at 11920 could create one that (upon
return) the code at line 550 could just refer to.
 
Back
Top