break C# with Example



break C# with Example

Sometimes loop condition should be checked in the middle of the loop. The former is arguably more elegant than 
the latter: 
for (;;) 
{ 
 

//  precondition  code  that  can  change  the  value  of  should_end_loop  expression 
if (should_end_loop) 
break; 
// do something 
} 
Alternative: 
bool endLoop = false; 
for (; !endLoop;) 
{ 
// precondition code that can set endLoop flag 
if (!endLoop) 
{ 
// do something 
} 
} 
Note: In nested loops and/or switch must use more than just a simple break. 
 

0 Comment's

Comment Form