468,306 Members | 1,279 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

for loop ending condition

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
3 3212

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

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
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.

Similar topics

11 posts views Thread by Wayne Folta | last post: by
1 post views Thread by LANkrypt0 | last post: by
23 posts views Thread by Mark Anderson | last post: by
10 posts views Thread by MariusI | last post: by
7 posts views Thread by UnkleVo | last post: by
33 posts views Thread by dmoran21 | last post: by
44 posts views Thread by James Watt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.