"continue" key word equivalent in VB

D

Daniel Bass

is there an equivalent key word for C++'s "continue" in VB (.net) in this
context?


CString szLine;
szLine = myReader.ReadLine();
while ( !szLine.IsEmpty() )
{
if ( szLine(0) == '-' )
{
szLine = myReader.ReadLine();
continue; // <-- HERE, so if szLine starts with "-" then
// the rest of this loop is not executed this
time round...
}

szLine = myReader.ReadLine();

//
// rest of the loop code
//

}


thanks!
Daniel.
 
C

Cor

Herfried,
Are you sure is it in that equivalent not something as exit while loop
But when you say so .................................................
Cor
 
J

Jay B. Harlow [MVP - Outlook]

Daniel,
The closest you will come to a Continue in VB.NET is to use the Goto
keyword.

Something like:
szLine = myReader.ReadLine()
Do Until szLine Is Nothing
If szLine.Chars(0) = "-"c Then
szLine = myReader.ReadLine()
GoTo continue
End If
szLine = myReader.ReadLine()
If szLine is Nothing Then Exit Do
'
' rest of the loop code
'
continue:
Loop

Personally I would put a single ReadLine after the Continue, avoiding the
third ReadLine. Also I would consider having the 'rest of the loop code' in
the Else block of the If comment block. Or change the If comment to If Not
comment rest of loop code.

Hope this helps
Jay
 
A

Adam Hart

is there an equivalent key word for C++'s "continue" in VB (.net) in this

There's always another way of writing it - how about instead of trying to =
'-', you test not equals to '-'

eg
while(not empty)
{
if(last char != '-')
{
rest of code in here
}
}

reads better too i think
 
D

Daniel Bass

There's always another way of writing it - how about instead of trying to
=
'-', you test not equals to '-'

eg
while(not empty)
{
if(last char != '-')
{
rest of code in here
}
}

reads better too i think

I'm afraid I have to disagree, nested parenthesis (or begin/end's in a VB
case) isn't that pleasing to the eye...

if your reading through the code, seeing a continue near the start signifies
a conditional loop, but seeing two sets of begin/end's can get confusing
when you're looking at something like

function ...()
{
while m_bWhatever
{
if ( my condition is true )
{
... code here ...
}
}
}

the last }'s can get confusing, especially in large multilevel functions.

it's all about preference I guess! ;o)

Thanks for your comment.
 
C

Cor

Jay,
Sorry, but this is terrible, all the work of Dijkstra (he died last year) is
done with what you wrote (while your examples are normaly so good).
szLine = myReader.ReadLine()
Do Until szLine Is Nothing
If szLine.Chars(0) = "-"c Then
szLine = myReader.ReadLine()
' GoTo continue /// removed
' End If /// removed
Else ' that is all Cor inserted
szLine = myReader.ReadLine()
If szLine is Nothing Then Exit Do
'
' rest of the loop code
'
' continue: ////removed
Loop
In C you have to keep track of the {} of course therefore escaping is very
easy.
That is exactly the same without using low level coding.
Cor
 
H

Herfried K. Wagner [MVP]

Hello Cor,

Cor said:
In C you have to keep track of the {} of course therefore escaping is very
easy.
That is exactly the same without using low level coding.

Mhmmm. I love GoTo.

HTH,
Herfried K. Wagner
 
H

Herfried K. Wagner [MVP]

Hello Daniel,

Daniel Bass said:
if your reading through the code, seeing a continue near the start signifies
a conditional loop, but seeing two sets of begin/end's can get confusing
when you're looking at something like

function ...()
{
while m_bWhatever
{
if ( my condition is true )
{
... code here ...
}
}
}

the last }'s can get confusing, especially in large multilevel functions.

\\\
function ...()
{
while m_bWhatever
{
if ( my condition is true )
{
... code here ...
} // if
} // while
} // function
///

;-)))

HTH,
Herfried K. Wagner
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
I was only demonstrating the continue.

The obscure last paragraph stated what I would actually do.
Personally I would put a single ReadLine after the Continue, avoiding the
third ReadLine. Also I would consider having the 'rest of the loop code' in
the Else block of the If comment block. Or change the If comment to If Not
comment rest of loop code.

As having the 'rest of the loop code in the else block' would avoid the
goto altogether. I should have added "And avoid the goto altogether".

However there are some loops used for parsing where you need the Goto, no
clean way around it. This example is simply to simple to demonstrate it.
There are ways to avoid these Goto/Continue statements however then you
start introducing intricate control variables with strange nested ifs, whose
only purpose is to avoid the goto. Where a single goto would avoid it.

Thanks for clarifying.

Jay
 
C

Craig Powers

Cor said:
Jay,
Sorry, but this is terrible, all the work of Dijkstra (he died last year) is
done with what you wrote (while your examples are normaly so good).

Dijkstra had a point but his generalization was about as valuable as
most (which is, often but not universally). I believe there have been
studies which have shown that flow control in loops is a valuable use
of goto -- it can produce cleaner and more maintainable code.

And let's be honest -- there are language features (most notably the
various "exit" statements) that are thinly disguised gotos.
 
C

Craig Powers

Daniel said:
is there an equivalent key word for C++'s "continue" in VB (.net) in this
context?

CString szLine;
szLine = myReader.ReadLine();
while ( !szLine.IsEmpty() )
{
if ( szLine(0) == '-' )
{
szLine = myReader.ReadLine();
continue; // <-- HERE, so if szLine starts with "-" then
// the rest of this loop is not executed this
time round...
}

szLine = myReader.ReadLine();

//
// rest of the loop code
//

}

I think this loop can probably be better cast.

My thought...

CString szLine;

do {
szLine = myReader.ReadLine();

while (!szLine.IsEmpty() && szLine(0) == '-')
szLine = myReader.ReadLine();

if (szLine.IsEmpty()) break;

// etc.
} while (! szLine.IsEmpty() );

Except that this isn't exactly the same -- do you really intend to
unconditionally discard the first line you read? That looks suspicious,
but it can be easily remedied with an initial ReadLine call outside
the loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top