This article reproduced: http://blog.chinaunix.net/uid-26715658-id-3254915.html
Break and Continue use the same range, both can be used for loops, and the break in It can also be used for switch. function also has certain similarity, break is equivalent to drop out, continue is equivalent to skip. For break, it is better to understand where the program jumps. But where the continue to go, the beginner may have some doubts, as it jumps to the back of the last sentence of the loop body.
If they are in a circle consisting of multiple loops and switch, they work on the innermost layer that includes them. As a result, people who imagine jumping out of multiple loops may not forget Goto.
The break statement cannot be used in a loop statement (such as a For loop or while loop) and in any other statement other than the switch statement.
The difference between the continue statement and the break statement is that the continue statement ends the loop only, rather than terminating the execution of the entire loop. The break statement ends the entire loop and no longer determines whether the condition of the execution loop is true.
Reference: The use of goto statements is not advocated since the late 60. ...... All programs that use Goto can be rewritten without a goto.
Note: Goto is a very controversial statement, the language of multi-book suggestions for less or not to use it, my personal habits are determined not to use. However, I do not know the term "the 60 's". Because I was learning BASIC for myself, it was 1994, when I learned that gw-basic,goto with line numbers was a must-have statement. Was it that our school curriculum was actually behind 20 years of content?
"Mistakes are made by programmers themselves, not Goto," says Dr Lin Rui, who has a different view of Goto. Goto has at least one place to show the avatar, it can jump from the multi-cycle suddenly to the outside, ... It's like a house on fire, too late to go down the stairs from the first level, can jump out of the fire pit from the window. (page 32nd of the high-quality C++/C Programming Guide)
The program I wrote has not yet surpassed the three-level cycle. Jumping from the innermost layer, if jumping one level, break, if jumping two or three layers, one is the probability is very small, second, if it really hit, I use other conditions to control whether the outer loop continues to break, since 1997 into the structured programming, I did completely abandon the Goto. Except for "On Error Goto" in--vb, there was an error, and naturally, no matter which layer, I jumped into error handling.
The goal of Goto is a label, the name of this label is somewhat interesting, because the label is only used for Goto, so its names can be the same as any variable name and other identifiers without having duplicate. The previous program has a line number, so the "goto line number", now the program does not have line numbers, but allows to be labeled anywhere. When the compiler touches them, it's probably the name of the colon behind it that doesn't need to be tested for legitimacy. So, the existing "public:" in C + + is not counted as a label?
To this end, I did an experiment: the experimental content I added a line of "pub:" In the declaration of the class, and two, I added a line "public:" in the program section. It turns out that neither of them can be compiled. That is, the experiment shows that the label is not allowed in places such as the class definition (it is not necessary because it is not inside any function, Goto is run-time, is not compiler-independent, and Goto does not allow cross-function skipping.) ), experiment two indicates that the label in the program section does not allow the use of reserved words.
The following examples illustrate :
#include
void Main ()
{
int n, m;
/* This loop outputs 1 2 3 4 and loops out when n is 5 */
for (n = 1; n <=; n + +)
{
if (n = = 5)
Break
printf ("%d", n);
}
printf ("\ n");
/* This loop Output 1 2 3 4 6 7*/
for (m = 1; M <=; m + +)
{
/* No other statements for the secondary loop are executed when M is 5, so no output 5*/
if (m = = 5)
Continue
/* When M is 8, the loop jumps to the RET, the end is not output, if it is break, can also output end*/
if (m = = 8)
{
Goto RET;
}
printf ("%d", m);
}
printf ("End");
Ret:
Return
}
The difference and usage of break, continue and Goto in C language