473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 3498

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.