Home  >  Article  >  Backend Development  >  What is the usage of continue in C language?

What is the usage of continue in C language?

烟雨青岚
烟雨青岚Original
2020-06-17 14:46:2715240browse

What is the usage of continue in C language?

#What is the usage of continue in C language?

The function of the continue statement is to skip the remaining statements in the loop body and force entry into the next loop.

The continue statement is only used in while and for loops, and is often used together with the if conditional statement to determine whether the condition is true.

Example

#include <stdio.h>
int main(){
    char c = 0;
    while(c!=&#39;\n&#39;){  //回车键结束循环
        c=getchar();
        if(c==&#39;4&#39; || c==&#39;5&#39;){  //按下的是数字键4或5
            continue;  //跳过当次循环,进入下次循环
        }
        putchar(c);
    }
    return 0;
}

Running result:

0123456789↙
01236789

When the program encounters while, the value of variable c is '\0', the loop condition c!='\n' is established, and the first loop starts. getchar() causes the program to pause execution, wait for user input, and does not start reading characters until the user presses the Enter key.

In this example, we input 0123456789. When 4 or 5 is read, the if condition c=='4'||c=='5' is established, and the continue statement is executed to end the current loop. , directly enter the next loop, which means putchar©; will not be executed. When other numbers are read, the if condition does not hold, the continue statement will not be executed, and putchar©; will output the read characters.

Comparison between break and continue: break is used to end all loops, and the loop statement no longer has a chance to be executed; continue is used to end this loop and jump directly to the next loop. If the loop condition is true, it will continue. Continue the cycle.

Recommended tutorial: "C Language"

The above is the detailed content of What is the usage of continue 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