Re: Strange behaviour



besides the difference between for and foreach the first example will always test each element of the array while the second example only tests until it finds the first non-negative.

Actually, the "i < a.Length && negative" in the loop means it will exit the loop as soon as it finds the first negative.

So, it is does come purely down to the difference between foreach() and for() loops ;-)

Personally, I would use the foreach version, as it is easier to read.
I'm not sure which would be better for performance

HTH


--
Ged Moretta
Senior Software Engineer
AppSense Ltd
www.appsense.com

-----------------------------------------------------------------------
This signature isn't automatic. I have to type it manually every time.
"Christof Nordiek" <cn@xxxxxxxxx> wrote in message news:OcMFxmi7HHA.5096@xxxxxxxxxxxxxxxxxxxxxxx
"Michele" <3rr0r200@xxxxxxxx> schrieb im Newsbeitrag news:op.txxqf4mo30xyjd@xxxxxxxxxxx
In data 31 agosto 2007 alle ore 17:49:28, Stanimir Stoyanov
One more question, it's a matter of style:

Which version of AllNegative a good programmer should use?

bool AllNegative(int[] a) {
bool negative = true;
for (int i = 0; i < a.Length && negative; i++)
negative = a[i]<0;
return negative;
}

bool AllNegative(int[] a) {
foreach (int x in a)
if (x>0) return false;
return true;
}


besides the difference between for and foreach the first example will always test each element of the array while the second example only tests until it finds the first non-negative.
This can have a huge performance impact, depending on the content and length of the arrays searched.

This is independant of for/foreach since also a for loop can be exited with return

Christof


.



Relevant Pages

  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: foreach statement output
    ... know about foreach loop workings. ... I've got an incoming array of unknown ... The Perl Cookbook ... My big confusion over this foreach issue is I failed to realize that the ...
    (comp.infosystems.www.authoring.cgi)
  • Re: safe to delete elements of array in foreach
    ... (normally this is not the case but not sure in php) ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, ...
    (comp.lang.php)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)