The statement in the program is usually always performed in the direction or in the direction defined by the statement. If you need to change the normal flow of the program, you can use the transfer statement introduced in this section. Four transfer statements are provided in the C language:
Goto, Break, Continue and Return.
The return statement can only appear in the modulated function, used to return to the primary, and we will introduce in the function chapter. This section describes the first three transfusion statements.
1.goto statement
The GOTO statement is also called unconditional transfer statement, and its general format is as follows: GOTO statement label; where the statement label is written according to the identifier, it is placed in front of a statement, and the reference number (:) is column after the reference number (:). The function of the statement is identified by the identifier, and uses the goto statement.
Such as: Label: i ; loop: while (x <7);
C language does not limit the number of times in the program, but the respective labels must not be renamed. The semantics of the GOTO statement are the statements that change the program flow direction and rotate the statement identified by the statement label.
The GOTO statement is usually used with the conditional statement. Can be used to achieve conditional transfer, constitute a loop, and jump out of the cyclic body.
However, in the structured programming, it is generally not advocated using a goto statement to avoid chaos of the program process, making it difficult to understand and debugging procedures.
Statistics Enter the number of characters 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 constitutes a loop structure with the IF statement and the GOTO statement. When the input character is not '/ n', the N is executed, and then transfer to the IF statement cycle execution. The loop is stopped until the input character is '/ n'.
BREAK statement
The BREAK statement can only be used in a switch statement or a loop statement, and its role is to jump out of the switch statement or jump out of this layer loop, and rotate the execution later. Since the direction of transfer of the BREAK statement is clear, the statement label is not required to cooperate with it. The general form of the Break statement is: BREAK; the BREAK statement is used in the Switch statement and the FOR statement in the above example. Using the BREAK statement can make the loop speech with multiple exits, making programming more flexible and convenient under some occasions.