Home >Backend Development >C#.Net Tutorial >The function of break in c language

The function of break in c language

下次还敢
下次还敢Original
2024-05-09 10:39:16576browse

The role of break in C language

Definition of break statement

The break statement is a control flow statement used to exit a loop or switch statement.

Using break in a loop

When the break statement appears within the loop body, it will cause the loop to terminate immediately and transfer control to the next line outside the loop. statement.

For example:

<code class="c">for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    printf("%d ", i);
}</code>

This code will print numbers from 0 to 4. When i equals 5, the break statement is executed, causing the loop to terminate.

Using break in a switch statement

When the break statement appears in the case branch of the switch statement, it will cause the switch statement to jump out and continue execution outside the case branch. Next statement.

For example:

<code class="c">switch (ch) {
    case 'a':
        printf("字母a");
        break;
    case 'b':
        printf("字母b");
        break;
    default:
        printf("其他字符");
}</code>

When ch equals 'a' or 'b', the break statement will be executed and the switch statement will jump out. When ch is not equal to 'a' or 'b', the default branch will be executed to print "other characters".

The above is the detailed content of The function of break in c language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn