The transfer statement of the C language primary lecture

Source: Internet
Author: User
Tags execution goto printf

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);
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.