Home:ALL Converter>Simplify nested loop logic

Simplify nested loop logic

Ask Time:2011-02-24T02:27:52         Author:PRASHANT P

Json Formatter

I am having some trouble trying to simplify some logic that is contained in nested loops

I have provided some pseudo code below, that is the basics of what I'm dealing with. The actual code is more complex and contains multiple nested loop structures.

I'm in charge of maintaining this code, I'm not the original writer of it

Would this be a place that a goto is valid.

while(CONDITION)
{
  while(CONDITITION2)
  {
    if(CONDITION3)
      break CONDITION2

    if(CONDITION4)
      break CONDITION
  }
}

There are many complexities to preforming these code breaks.

I am not allowed to use a goto, but I think it would be an easier solution that what I am currently trying to do. Should I try and justify the use of a goto in this case. thanks

PRASHANT :)

Author:PRASHANT P,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5095312/simplify-nested-loop-logic
Mark Sowul :

You're setting yourself up for messy and complicated code. Consider LINQ if possible (loops themselves are practically a code smell these days, especially complicated ones), otherwise consider refactoring the inner loop into a separate method and using return;. \n\nEdit: great post by Eric Lippert critiquing the various alternatives for this problem: http://blogs.msdn.com/b/ericlippert/archive/2010/01/11/continuing-to-an-outer-loop.aspx",
2011-02-23T18:33:30
Cokegod :

Use a variable for condition 1:\n\nbool condition1 = true;\nwhile (CONDITION && condition1)\n{\n while (CONDITION2)\n {\n if (CONDITION3)\n break;\n if (CONDITION4)\n {\n condition1 = false;\n break;\n }\n }\n}\n",
2011-02-23T18:32:06
corsiKa :

When Java (not C#, but relevant) made the decision to disallow goto, they found through code profiling that 90% of all gotos were used for loops, and the rest were used for really bad stuff.\n\nBreak and continue still exist because they're legitimate, localized gotos that work. Make sure to document where they go and why they do it, but they're still very useful.\n\nYou appear to be using them in the most effective way.",
2011-02-23T18:32:16
Austin Salonen :

Even without fully knowing the context, I can say I would hate to be the next programmer on that code.\n\nLINQ could probably simplify it some. If you're using ReSharper, it will suggest it for you if it can.\n\nBeyond that, I would put some more thought into how I was getting to these nested loops in the first place and try to get rid of as many as possible. A better object model or a smart use of patterns could get you a better and more maintainable solution.",
2011-02-23T18:56:04
Femaref :

Introduce a variable\n\nbool breakOuter = false;\nwhile(CONDITION)\n{\n while(CONDITITION2)\n {\n if(CONDITION3)\n break;\n\n if(CONDITION4)\n {\n breakOuter = true;\n break;\n }\n }\n if(breakOuter)\n break;\n}\n",
2011-02-23T18:30:30
yy