Connecting Tech Pros Worldwide Forums | Help | Site Map

for loop ending condition

Ben R.
Guest
 
Posts: n/a
#1: Nov 21 '05
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

Larry Lard
Guest
 
Posts: n/a
#2: Nov 21 '05

re: for loop ending condition



Ben R. wrote:[color=blue]
> In an article I was reading
>[/color]
(http://www.ftponline.com/vsm/2005_06...topdeveloper/),[color=blue]
>
> I read the following:
>
> "The ending condition of a VB.NET for loop is evaluated only once,[/color]
while the[color=blue]
> 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[/color]
would you[color=blue]
> know whether or not to exit?[/color]

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

Larry Lard
Guest
 
Posts: n/a
#3: Nov 21 '05

re: for loop ending condition



Ben R. wrote:[color=blue]
> In an article I was reading
>[/color]
(http://www.ftponline.com/vsm/2005_06...topdeveloper/),[color=blue]
>
> I read the following:
>
> "The ending condition of a VB.NET for loop is evaluated only once,[/color]
while the[color=blue]
> 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[/color]
would you[color=blue]
> know whether or not to exit?[/color]

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

Ben R.
Guest
 
Posts: n/a
#4: Nov 21 '05

re: for loop ending condition


Thanks Larry. Your response could not have been any clearer...

-Ben

"Larry Lard" wrote:
[color=blue]
>
> Ben R. wrote:[color=green]
> > In an article I was reading
> >[/color]
> (http://www.ftponline.com/vsm/2005_06...topdeveloper/),[color=green]
> >
> > I read the following:
> >
> > "The ending condition of a VB.NET for loop is evaluated only once,[/color]
> while the[color=green]
> > 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[/color]
> would you[color=green]
> > know whether or not to exit?[/color]
>
> 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
>
>[/color]
Closed Thread