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