4.4.4 Loop Interrupt
Sometimes more granular control over the processing of the loop code is required. C # provides 4 commands for this, 3 of which have already been described in other scenarios:
? break--immediately terminates the loop.
? continue--immediately terminates the current loop (resumes execution of the next loop).
? goto--can jump out of the loop to a marked position (it's best not to use the command if you want the code to be easy to read and understand).
? return--jumps out of the loop and its included functions (see Chapter 6th).
? The break command exits the loop and proceeds to the first line of code following the loop
4.4.5 Infinite Loop
You can define a loop that never terminates, known as an infinite loop, by writing error codes or wrong designs. For example, the following code:
while (true) { //
This code is also useful when using the break statement or manually using the Windows Task Manager to always exit such loops. However, when this happens occasionally, there is a problem. Consider the following loop, which is very similar to the for loop in the previous section:
int 1 while) { if20) Continue; Console.WriteLine ("{0}", i++
I is incremented only after the last line of the loop has been executed, that is, after the continue statement has finished executing. However, when executing to this continue statement (at this point I is 2), the program will use the same I value for the next loop, and then test the I value, continue the loop, always go on. This freezes the application.
(original) C # learning Note 04--Process Control 04--cyclic 04--cyclic interrupts and infinite loops