By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,507 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,507 IT Pros & Developers. It's quick & easy.

for loop ending condition

P: n/a
In an article I was reading
(http://www.ftponline.com/vsm/2005_06...topdeveloper/),

I read the following:

"The ending condition of a VB.NET for loop is evaluated only once, while the
C# for loop ending condition is evaluated on every iteration."

Is this accurate? I don't understand how you could get away without
evaluating the ending condition at every iteration. Otherwise, how would you
know whether or not to exit?

-Ben
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Ben R. wrote:
In an article I was reading
(http://www.ftponline.com/vsm/2005_06...topdeveloper/),
I read the following:

"The ending condition of a VB.NET for loop is evaluated only once, while the C# for loop ending condition is evaluated on every iteration."

Is this accurate? I don't understand how you could get away without
evaluating the ending condition at every iteration. Otherwise, how would you know whether or not to exit?


This is arguably accurate, but misleading. (What follows assumes
knowledge of C#)

The correct VB.NET construction to compare a C# for loop to is not a
For ... Next loop. Let us review a C# for loop:

for ( <initial statement> ; <loop continuation condition> ; <step
statement> )
<statment block>

[no nitpicks please :)]

Note that the initial statement and step statement can be ANY statement
you like. What this is equivalent to is a particular form of the VB.NET
Do ... Loop loop:

<initial statement>
Do While <loop continuation condition>
<statment block>

<step statement>
Loop

Whereas a VB.NET For ... Next loop:

For <index> = <start value> To <end value> Step <step value>
<statement block>
Next

is equivalent to this, *different*, Do ... Loop loop:

<index = start value>
<CONST end value = whatever>
Do While <index <= end value>
<statement block>

<index += step value>
Loop

That CONST is effectively what the referenced article is talking about:
With a VB.NET For ... Next loop, the end value is evaluated ONCE, and
for the whole time the loop is looping, the loop continuation condition
is 'index <= end value'. With a C# for() loop, the actual *expression*
supplied as the loop continuation condition is evaluated EVERY TIME
around.

Example:

VB.NET:

Dim endvalue as integer = 10
dim i as integer

for i = 1 to endvalue
msgbox(i.tostring)
endvalue \= 2
next

'Because endvalue is evaluated ONCE at the start of the loop,
'you will see TEN messageboxes

C#:

int ev=10;
int i;

for(i=1; i<=ev; i++)
{
MessageBox.Show (i.ToString());
ev /=2;
}

// because "i<=ev" is evaluated EVERY TIME ROUND,
// you will see only TWO message boxes

Make sense?

--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a

Ben R. wrote:
In an article I was reading
(http://www.ftponline.com/vsm/2005_06...topdeveloper/),
I read the following:

"The ending condition of a VB.NET for loop is evaluated only once, while the C# for loop ending condition is evaluated on every iteration."

Is this accurate? I don't understand how you could get away without
evaluating the ending condition at every iteration. Otherwise, how would you know whether or not to exit?


This is arguably accurate, but misleading. (What follows assumes
knowledge of C#)

The correct VB.NET construction to compare a C# for loop to is not a
For ... Next loop. Let us review a C# for loop:

for ( <initial statement> ; <loop continuation condition> ; <step
statement> )
<statment block>

[no nitpicks please :)]

Note that the initial statement and step statement can be ANY statement
you like. What this is equivalent to is a particular form of the VB.NET
Do ... Loop loop:

<initial statement>
Do While <loop continuation condition>
<statment block>

<step statement>
Loop

Whereas a VB.NET For ... Next loop:

For <index> = <start value> To <end value> Step <step value>
<statement block>
Next

is equivalent to this, *different*, Do ... Loop loop:

<index = start value>
<CONST end value = whatever>
Do While <index <= end value>
<statement block>

<index += step value>
Loop

That CONST is effectively what the referenced article is talking about:
With a VB.NET For ... Next loop, the end value is evaluated ONCE, and
for the whole time the loop is looping, the loop continuation condition
is 'index <= end value'. With a C# for() loop, the actual *expression*
supplied as the loop continuation condition is evaluated EVERY TIME
around.

Example:

VB.NET:

Dim endvalue as integer = 10
dim i as integer

for i = 1 to endvalue
msgbox(i.tostring)
endvalue \= 2
next

'Because endvalue is evaluated ONCE at the start of the loop,
'you will see TEN messageboxes

C#:

int ev=10;
int i;

for(i=1; i<=ev; i++)
{
MessageBox.Show (i.ToString());
ev /=2;
}

// because "i<=ev" is evaluated EVERY TIME ROUND,
// you will see only TWO message boxes

Make sense?

--
Larry Lard
Replies to group please

Nov 21 '05 #3

P: n/a
Thanks Larry. Your response could not have been any clearer...

-Ben

"Larry Lard" wrote:

Ben R. wrote:
In an article I was reading

(http://www.ftponline.com/vsm/2005_06...topdeveloper/),

I read the following:

"The ending condition of a VB.NET for loop is evaluated only once,

while the
C# for loop ending condition is evaluated on every iteration."

Is this accurate? I don't understand how you could get away without
evaluating the ending condition at every iteration. Otherwise, how

would you
know whether or not to exit?


This is arguably accurate, but misleading. (What follows assumes
knowledge of C#)

The correct VB.NET construction to compare a C# for loop to is not a
For ... Next loop. Let us review a C# for loop:

for ( <initial statement> ; <loop continuation condition> ; <step
statement> )
<statment block>

[no nitpicks please :)]

Note that the initial statement and step statement can be ANY statement
you like. What this is equivalent to is a particular form of the VB.NET
Do ... Loop loop:

<initial statement>
Do While <loop continuation condition>
<statment block>

<step statement>
Loop

Whereas a VB.NET For ... Next loop:

For <index> = <start value> To <end value> Step <step value>
<statement block>
Next

is equivalent to this, *different*, Do ... Loop loop:

<index = start value>
<CONST end value = whatever>
Do While <index <= end value>
<statement block>

<index += step value>
Loop

That CONST is effectively what the referenced article is talking about:
With a VB.NET For ... Next loop, the end value is evaluated ONCE, and
for the whole time the loop is looping, the loop continuation condition
is 'index <= end value'. With a C# for() loop, the actual *expression*
supplied as the loop continuation condition is evaluated EVERY TIME
around.

Example:

VB.NET:

Dim endvalue as integer = 10
dim i as integer

for i = 1 to endvalue
msgbox(i.tostring)
endvalue \= 2
next

'Because endvalue is evaluated ONCE at the start of the loop,
'you will see TEN messageboxes

C#:

int ev=10;
int i;

for(i=1; i<=ev; i++)
{
MessageBox.Show (i.ToString());
ev /=2;
}

// because "i<=ev" is evaluated EVERY TIME ROUND,
// you will see only TWO message boxes

Make sense?

--
Larry Lard
Replies to group please

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.