tags:

views:

576

answers:

6

How do I force a loop to check its condition after every line of execution instead of only when the entire block finishes?

I have a while(!statement) { } loop, but statement can be changed by several different methods and should force the loop to break immediately after the current line in the loop has finished executing; instead of when the entire loop block has completed a cycle.

Is there any way to do this?

+3  A: 

yes, but you won't like it... just put a if (!condition) break; after each instruction ;)

Thomas Levesque
This is why I miss not having C++ macros in C#.
AaronLS
+3  A: 

This is not how the language works. You will have to check at regular intervals:

while (processing)
{
    actionA();

    if (!processing)
        break;

    actionB();

    if (!processing)
        break;

    actionC();
}
Richard Szalay
+1  A: 

I think that you would need an if statement after every line that could change the value, with a break if the condition was met.

Shiraz Bhaiji
+11  A: 

Depending on what you are doing, you could consider having each statement be a delegate, create an array of delegates, and run a for loop across the array. That way the loop is just two lines: one that checks the condition, and when that executes the delegate at the current array position.

AaronLS
Delegates would not work for this situation. I instead opted to restructure the loop's contents and put checks at strategic points. Thanks to everyone for your replies.
Scorpion
If this answer did not work for you, why did you mark it as accepted? (sorry aaronls)
Roee Adler
Can you explain why delegates would not work in this situation? I included my solution anyway for anyone else that might find it useful: http://stackoverflow.com/questions/1218846/c-force-loop-to-check-condition/1219204#1219204
Robert Cartaino
+3  A: 

What about having those methods throw a custom exception and put a try-catch for the specific exception within your loop.

while (!condition) {
     try {
         //method calls
     } catch (CustomException ce) {
         break;
     }
    }
Ryan Ische
+1 because depending on the condition(perhaps error condition) this might be appropriate. I would kind of avoid it though if the condition is not something you'd except an exception to arise in.
AaronLS
-1 use exceptions to handle exceptional circumstances, not to control flow.
Matt Howells
As with most things in programming, use something where it makes sense. I agree that using exceptions for control flow is bad, but in the OP's circumstances it could be a practical solution.
Ryan Ische
+2  A: 

You could create a custom iterator (using the yield statement). The iterator would return each line (or multiple lines) you want to execute (using anonymous methods). You could then iterate through each line one at a time and check the condition inside the loop. Here is how it would look:

public delegate void DelegateType();

public static IEnumerable< DelegateType > GetStatements()
{
    // ---- replace with your code below ----
    yield return delegate() { Console.WriteLine("statement 1"); };
    yield return delegate() { Console.WriteLine("statement 2"); };
    yield return delegate() { Console.WriteLine("statement 3"); };
    yield return delegate() 
    { 
        // You can return multiples statements in one block.
        Console.WriteLine("statement 4"); 
        Console.WriteLine("statement 5");
    };
}

Here is how you would iterate through your statements and check the condition after each statement.

IEnumerable<DelegateType> statementList = GetStatements();
foreach (DelegateType statement in statementList)
{
    statement();                   // Here is where your statement executes.
    if (!ConditionContinue())      // Check your condition here.
    {
        break;
    }
}

Enjoy,

Robert C. Cartaino

Robert Cartaino