C break and continue language usage Detailed (out of the loop)

When you use while or for loop, if you want to advance the end of the cycle (end condition is not satisfied in the case of the end of the cycle), you can use the break or continue keywords.

break keyword

break, use it to jump out of the switch statement.

When the break keyword is used while, for loop, the loop will terminate and execute the entire back of the loop code. break keywords are usually used with the if statement, then out of the loop when the condition is met.

While loop 100 calculates a value 1 is added:

#include <stdio.h>
int main(){
    int i=1, sum=0;
    while(1){  //循环条件为死循环
        sum+=i;
        i++;
        if(i>100) break;
   }
    printf("%d\n", sum);
    return 0;
}

The result:
5050

while cycling conditions were 1, it is an infinite loop. When performing the 100th cycle, the complete calculation i ++; the value of i is 101, the conditions at this time if statement i> 100 true, execute break; statement, the end of the cycle.

In the multilayer loop, a break only one jump outward. For example, the output integer matrix of a 4 * 4:

#include <stdio.h>
int main(){
    int i=1, j;
    while(1){  // 外层循环
        j=1;
        while(1){  // 内层循环
            printf("%-4d", i*j);
            j++;
            if(j>4) break;  //跳出内层循环
        }
        printf("\n");
        i++;
        if(i>4) break;  // 跳出外层循环
    }

    return 0;
}

The results:
. 1. 4. 3 2
2. 8. 6. 4
. 3. 6. 9 12 is
. 4. 8 12 is 16

When j> 4 true, execute break ;, out of the inner loop; outer loop is still executed until i> 4 set up, out of the outer loop. The inner loop is performed a total of four times, the outer loop is performed a total of 1 Ci.

continue Statement

Is a skip action continue statement loop statement and the remaining forced into the next cycle. continue statement only in while, for circulation, often used in conjunction with the if condition, the determination condition is satisfied.

Look at an example:

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

Operating results:
0123456789↙
01,236,789

When the program encounters while, the variable c is '\ 0', cycling conditions c! = '\ N' was established and began the first cycle. getchar () make the program be suspended, waiting for user input until the user presses the Enter key began to read characters.

We present embodiment is inputted 0123456789, when read 4 or 5, IF condition c == '4' || c == '5' was established, continue statement is executed, the end of the current cycle, the next cycle directly into , that putchar ©; will not be executed. And when reading to other numbers, if the condition is not satisfied, continue statement will not be executed, putchar ©; output will read characters.

Comparison of break and continue: break to the end of all cycles, loop no longer have a chance to execute; continue to the end of this cycle, skip ahead to the next cycle, if the loop condition is satisfied, the cycle will continue.

Published 48 original articles · won praise 0 · Views 317

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/105369154