|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
for loop ending conditionIn an article I was reading
(http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), 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 Ben R. wrote:
> In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/),> > This is arguably accurate, but misleading. (What follows assumes> 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? 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 Thanks Larry. Your response could not have been any clearer...
-Ben Show quoteHide quote "Larry Lard" wrote: > > Ben R. wrote: > > In an article I was reading > > > (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), > > > > 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 > > Ben R. wrote:
> In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/),> > This is arguably accurate, but misleading. (What follows assumes> 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? 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
DataReader in Excel (Better Post than the last)
Update for VS .NET 2003? Compiler errors due to temp file inconsistencies ?? Remove items from listbox How to use Regex to Parse this String How to get assembly file info? cast sortedlist value as hashtable Prevent User Entering New Values in ComboBox Date validation / verification expand a node of a treeview programmatically |
|||||||||||||||||||||||