Statements in a program are usually performed in sequential direction, or in the direction defined by the statement function. If you need to change the normal flow of your program, you can use the transfer statement described in this section. There are 4 kinds of transfer statements in the C language:
Goto,break, continue and return.
The return statement can only appear in the called function, which returns the keynote function, which we will describe in the function chapter. This section describes the first three transfer statements.
1.goto statement
A goto statement is also called an unconditional transfer statement, and its general format is as follows: Goto statement label, where the statement label is a symbol written according to the identifier, placed in a statement line
Preceded by a colon (:) after marking. The statement label acts as an identifying statement and is used in conjunction with the GOTO statement.
such as: label:i++;
Loop:while (X<7);
C language does not restrict the use of the number of labels in the program, but each label must not duplicate. The semantics of a goto statement is to change the flow of the program and to execute the statement identified by the statement label.
Goto statements are usually used in conjunction with conditional statements. can be used to achieve conditional transfer, forming a cycle, jump out of the circulation body and other functions.
However, the use of GOTO statements in structured programming is not generally advocated, so as to avoid the confusion of program flow, which makes it difficult to understand and debug the program.
Counts the number of line characters entered from the keyboard.
#include"stdio.h"
void main(){
int n=0;
printf("input a string\n");
loop: if(getchar()!='\n')
{
n++;
goto loop;
}
printf("%d",n);
} int n=0;
printf("input a string\n");
loop: if(getchar()!='\n')
{
n++;
goto loop;
}
printf("%d",n);
This example uses an if statement and a goto statement to form a looping structure. When the input character is not ' \ n ', the n++ is counted and then transferred to the IF statement for loop execution. The loop is not stopped until the input character is ' \ n '.
Break statement
A break statement can only be used in a switch statement or Loop statement, which is to jump out of a switch statement or loop out of this layer, and then go to the following program. Because the transfer direction of the break statement is clear, it does not need to match the statement label. The general form of the break statement is: break; The break statement is used as a jump in the switch statement and the For statement, respectively, in the example above. Use the break statement to make the loop statement have multiple exits, making programming more flexible and convenient in some cases.
Continue statement
The continue statement can only be used in the loop body, and its general format is:
Continue
Its semantics is: to end this cycle, that is, no longer execute the sentence after the continue statement in the circulation body, to the judgment and execution of the next cyclic condition. It should be noted that this statement only ends this layer of the loop, does not jump out of the loop.
void main(){
int n;
for(n=7;n<=100;n++)
{
if (n%7!=0)
continue;
printf("%d ",n);
}
}