Essential Knowledge Points for C Language Beginners: Summary of break and continue Usage Techniques

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute each day to remember the basics of C language.

Series of 100 Essential Knowledge Points for C Language Beginners

30. Summary of break and continue Usage Techniques: Loop Control Statements

1. Detailed Explanation of break Statement

1. Basic Function and Syntax

The break statement is used to immediately terminate the execution of the current loop or switch statement:

for (int i = 0; i < 10; i++) {
    if (i == 5) break;  // Exit the loop when i equals 5
    printf("%d ", i);
}
// Output: 0 1 2 3 4

2. break in Nested Loops

The break only affects the innermost loop:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 5; j++) {
        if (j == 2) break;  // Only exit the inner loop
        printf("%d-%d ", i, j);
    }
}
/* Output:
0-0 0-1 
1-0 1-1 
2-0 2-1 */

2. Detailed Explanation of continue Statement

Basic Syntax

The continue statement skips the remaining part of the current loop and directly proceeds to the next iteration:

for (int i = 0; i < 5; i++) {
    if (i == 2) continue;  // Skip the case when i=2
    printf("%d ", i);
}
// Output: 0 1 3 4

2. continue in while Loops

int i = 0;
while (i < 5) {
    i++;
    if (i == 3) continue;  // Skip but i has already incremented
    printf("%d ", i);
}
// Output: 1 2 4 5

3. Key Differences Comparison

Feature break continue
Loop Control Completely terminates the loop Skips the current iteration
Applicable Structures Loops and switch Only loop structures
Nested Loops Affects only the innermost Affects only the innermost

4. Common Mistakes

Infinite Loop Trap

int i = 0;
while (i < 10) {
    if (i == 5) continue;  // Skips i++ causing an infinite loop
    printf("%d ", i);
    i++;
}

Correction:

while (i < 10) {
    if (i == 5) { i++; continue; }
    printf("%d ", i++);
}

Some students contacted me, wanting to have a study exchange group. Previously, I hesitated to create one due to concerns about advertisements, but I think having a group would indeed be more convenient, so I will try to set one up this time.

If you need it, hurry up and join, valid for 7 days.

Essential Knowledge Points for C Language Beginners: Summary of break and continue Usage Techniques

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet, please feel free to use them, the views are for learning reference only, if there are any errors, please forgive me~~]

Essential Knowledge Points for C Language Beginners: Summary of break and continue Usage Techniques

Essential Knowledge Points for C Language Beginners: Summary of break and continue Usage Techniques

“If you like C, please like it”Essential Knowledge Points for C Language Beginners: Summary of break and continue Usage Techniques Click the bottom right corner to viewEssential Knowledge Points for C Language Beginners: Summary of break and continue Usage Techniques

Leave a Comment