for loop
-
How this happend to you ever? In the Programing world you use break to break out of a loop but in the real world you need a break to really continue.
I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.
-
How this happend to you ever? In the Programing world you use break to break out of a loop but in the real world you need a break to really continue.
-
How this happend to you ever? In the Programing world you use break to break out of a loop but in the real world you need a break to really continue.
Smart-Not-Clever wrote:
n the real world you need a break to really continue.
What's she done?
I wanna be a eunuchs developer! Pass me a bread knife!
-
I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.
Think of it as a switch with an iterable. It has to break the iteration when it's finished doing what it has to do, or a millisecond or two of time will be lost forever.
I wanna be a eunuchs developer! Pass me a bread knife!
-
I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.
I suppose that is in the same set of 'never use goto' and 'no multiple return'. That's a rather dogmatic approach that may work possibly with newbies, in my humble opinion. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
How this happend to you ever? In the Programing world you use break to break out of a loop but in the real world you need a break to really continue.
-
I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.
With linq to sql I do this with
foreach
loops for performance reasons... Allow me to elaborate with two examples: Option A:var queryResult = (from i in ctx.SomeTable
where i.ID == 42
select i);//
// Only continue if queryResult has an item
if (queryResult.Count() > 0)
{
//
// Do some work
}Option B:
var queryResult = (from i in ctx.SomeTable
where i.ID == 42
select i);//
// Only continue if queryResult has an item
bool ItemExsists = false;
foreach (var i in queryResult)
{
ItemExsists = true;
break;
}if (ItemExsists)
{
//
// Do Some Work
}queryResult.Count()
is dead frign slow when all your interested in knowing is if there is at least one result in the collection. Option B isn't very pretty, but it works better than option A speed wise.Harvey Saayman - South Africa Software Developer .Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111 -
With linq to sql I do this with
foreach
loops for performance reasons... Allow me to elaborate with two examples: Option A:var queryResult = (from i in ctx.SomeTable
where i.ID == 42
select i);//
// Only continue if queryResult has an item
if (queryResult.Count() > 0)
{
//
// Do some work
}Option B:
var queryResult = (from i in ctx.SomeTable
where i.ID == 42
select i);//
// Only continue if queryResult has an item
bool ItemExsists = false;
foreach (var i in queryResult)
{
ItemExsists = true;
break;
}if (ItemExsists)
{
//
// Do Some Work
}queryResult.Count()
is dead frign slow when all your interested in knowing is if there is at least one result in the collection. Option B isn't very pretty, but it works better than option A speed wise.Harvey Saayman - South Africa Software Developer .Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111A foreach loop is more like a while loop than a for loop, so you have more slack to use break. Then, what about Option C?
if (queryResult.Any())
{
//do some work.
} -
I normally take a very hard look at a for loop that needs a break, wondering who and why TF wrote such a thing.
-
I suppose that is in the same set of 'never use goto' and 'no multiple return'. That's a rather dogmatic approach that may work possibly with newbies, in my humble opinion. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I like to add a goto inside my empty exceptions which then jump to a switch statement with multiple returns.
Todd Smith
-
I often use break when there's one exception to the rule which is the loop condition. Including that extra exception into the loop declaration just makes it unclearer in my mind.
I always make exceptions as well. Exceptions rule.
-
I like to add a goto inside my empty exceptions which then jump to a switch statement with multiple returns.
Todd Smith
So you are not a newbie. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]