C Primer Plus (5th Edition)
| I l @ ve RuBoard |
Summary: Program Jumps
Keywords
The keywords for program jumps are break , continue , and goto .
General Comments
The three instructions break , continue , and goto cause program flow to jump from one location of a program to another location.
The break Command
The break command can be used with any of the three loop forms and with the switch statement. It causes program control to skip the rest of the loop or switch containing it and to resume with the next command following the loop or switch .
Example
switch (number) { case 4: printf("That's a good choice.\n"); break; case 5: printf("That's a fair choice.\n"); break; default: printf("That's a poor choice.\n"); }
The continue Command
The continue command can be used with any of the three loop forms, but not with a switch . It causes program control to skip the remaining statements in a loop. For a while or for loop, the next loop cycle is started. For a do while loop, the exit condition is tested and then, if necessary, the next loop cycle is started.
Example
while ((ch = getchar()) != EOF) { if (ch == ` ') continue; putchar(ch); chcount++; }
This fragment echoes and counts nonspace characters .
The goto Command
A goto statement causes program control to jump to a statement bearing the indicated label. A colon is used to separate a labeled statement from its label. Label names follow the rules for variable names . The labeled statement can come either before or after the goto .
Form
goto label ; label : statement
Example
top : ch = getchar(); if (ch != `y') goto top;
| I l @ ve RuBoard |