472,778 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,778 software developers and data experts.

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 3443

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Wayne Folta | last post by:
Two observations about PEP-315: 1. It's clever, addresses a definite "wart", and is syntactically similar to try/except. But it's syntax seems like an acquired taste to me. 2. It is a very...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
1
by: LANkrypt0 | last post by:
I wrote the following code and for some reason it does not exit when my $word is the same as $ARGV. Can anyone shed some light on this for me? Is it because it needs to actually run through...
23
by: Mark Anderson | last post by:
A 'for' loop takes 3 arguments (initialize; test; increment). The 'test' must equate as true or false This doesn't work... x = 5; for (y=1; (y==5); y+=1) { alert(x * y); } ...nor does... x...
10
by: MariusI | last post by:
I stumbled over an optimization (or lack of one, to be specific) when viewing IL opcodes generated by the compiler using ms .net 2003. I was testing fast pixel manipulation using Bitmap.LockBits...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
7
by: UnkleVo | last post by:
Can someone run the code below and tell me why it never reaches 0.06? I am really puzzled..... or just going crazy? Dim i As Double For i = 0.01 To 0.05 Step 0.01 Debug.WriteLine(i) Next...
33
by: dmoran21 | last post by:
Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.