Re: Anything wrong with the way I use "break"s in my loops?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



There isn't anything logically wrong the way you use "break" twice to get out of the 2 loops, but a few lines of tedious code. You can make the code a bit more concise by immediately returning the found value in the inner loop:

foreach (...)
{
....
foreach (BMarkUnit bu in bp.AggregateLists)
{
if ( bu.ListName == list )
{
return bu.OriginalWeight;
}
}
}

As for execution efficientcy, I do not think it makes noticeable difference.


"Curious" <fir5tsight@xxxxxxxxx> wrote in message news:11e6757d-66b9-4491-b5ca-474b1f7bd59a@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have a simple method that does a search. Details below:

"mBenchmarkPriceList" is an ArrayList with each element being of
"BenchmarkPrice" type. There are three members in "BenchmarkPrice":
Ticker, Side, and AggregateLists.
The member, "AggregateLists", however, is an ArrayList with each
element being a "BMarkUnit" type. "BMarkUnit" contains two members,
ListName and OriginalWeight.

The method, "GetOriginalWeight", searches through the entire
"mBenchmarkPriceList". If a match of Ticker, Side, and also ListName
is found, it will return "OriginalWeight".

In order to be efficient, once a match is found, I "break" both loops
and return the value for "OriginalWeight". However, it seems that an
incorrect value is returned most of the time. Is there anything wrong
with the way I use either "break"?

Thanks!

double GetOriginalWeight ( string list, string ticker, string
side )
{
double origWeight = 0.0;
bool found = false;

foreach (BenchmarkPrice bp in mBenchmarkPriceList)
{

if ( bp.Ticker == ticker &&
bp.Side == side || ( side == "Buy" && bp.Side ==
"Cover" ) || ( side == "Sell" && bp.Side == "Short" ))
{
foreach (BMarkUnit bu in bp.AggregateLists)
{
if ( bu.ListName == list )
{
origWeight = bu.OriginalWeight;

found = true;
break;
}
}
}

if (found)
{
break;
}
}

return origWeight;
}

.



Relevant Pages

  • Re: foreach enhancement
    ... Additionally we were talking about stating increment values for cases like ... Extra syntax would be required and ... project only to make its loops look better and perform much slower. ... But if you simply could write foreach wouldn't you used ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# very optimisation
    ... >>> Another way to make your code faster is to never use foreach loops. ... where the iteration is actually the bottleneck. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach loops are sooooo tricky.....
    ... loops are not at all like for loops, ... Translating Fortran to PHP, because hosters won't allow anything else ... I wish PHP would do array and matrix stuff like Fortran or C, ... I find foreach loops to be quite intuitive. ...
    (comp.lang.php)
  • Re: foreach enhancement
    ... >> foreach ) ... > Loops are one of the most common things in programming so making them more ... deserves *no* extra syntax. ... Have you ever designed a compiler? ...
    (microsoft.public.dotnet.languages.csharp)