Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Force loop to check condition

Tags:

c#

loops

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?

like image 717
Scorpion Avatar asked Nov 28 '22 12:11

Scorpion


2 Answers

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.

like image 66
AaronLS Avatar answered Dec 15 '22 02:12

AaronLS


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

like image 38
Thomas Levesque Avatar answered Dec 15 '22 03:12

Thomas Levesque