Control statements in C language (2)

Table of contents

 do while()

continue()

break


 do while()


In C language, `do...while` loop is a loop structure, which is similar to `while` loop, but there is a key difference: `do...while` loop guarantees that the loop body is executed at least once, and then Test whether to continue execution based on conditions. The general syntax of a `do...while` loop is as follows:

do {

    // Loop body

} while (condition);

- The `loop body` is a section of code that is executed on each loop iteration and contains the operations to be performed repeatedly.

- `condition` is a boolean expression that tests whether the loop continues execution. Unlike a `while` loop, a `do...while` loop executes the loop body first and then checks the condition. The loop will continue executing as long as the condition is true (non-zero); once the condition is false (zero), the loop will exit.

Here is an example that demonstrates the use of a `do...while` loop:

#include <stdio.h>

int main() {

    int count = 1;

    do {

        printf("This is the %dth loop.\n", count);

        count++;

    } while (count <= 5);

    return 0;

}

In the above example, the `do...while` loop first executes the loop body and then checks the condition `count <= 5`. Even if the condition is false initially, the body of the loop will still be executed once. Then, on each iteration, `count` is incremented once until the value of `count` is no longer less than or equal to 5, at which point the loop terminates.

The `do...while` loop is suitable for scenarios where the loop body needs to be executed at least once and then continues execution based on conditions. It is more suitable for certain problems than `while` loop. Please make sure to set the conditions correctly when using `do...while` loops to avoid infinite loops.

continue()


In C language, `continue` is a control statement used inside a loop to skip the current iteration and continue with the next iteration. When the `continue` statement is executed, it causes the program to skip the remaining code of the current iteration and go directly to the next loop iteration. `continue` is usually used for conditional judgment inside a loop to skip specific conditions.

The `continue` statement is used as follows:

for (int i = 1; i <= 5; i++) {

    if (condition) {

        // When the conditions are met, execute continue and skip the current iteration

        continue;

    }

    //The code here is executed when the conditions are not met

}

Or used in `while` loops and `do...while` loops:

while (condition) {

    // some code

    if (some_condition) {

        // When the conditions are met, execute continue and skip the current iteration

        continue;

    }

    // more code

}

do {

    // some code

    if (some_condition) {

        // When the conditions are met, execute continue and skip the current iteration

        continue;

    }

    // more code

} while (condition);

Here is an example demonstrating the use of `continue`:

#include <stdio.h>

int main() {

    for (int i = 1; i <= 5; i++) {

        if (i == 3) {

            printf("Skip the 3rd iteration.\n");

            continue; // Skip the current iteration

        }

        printf("This is the %d iteration.\n", i);

    }

    return 0;

}

In this example, when the value of `i` equals 3, the `continue` statement is executed, skipping the remaining code of the 3rd iteration, and the program continues with the next iteration.

The `continue` statement is often used with conditional statements to skip certain iterations under specific conditions.

break


In C language, `break` is a control statement used to end the execution of a loop or `switch` branch early inside a loop or in a `switch` statement, that is, to jump out of the current loop or branch and continue execution after the loop or branch. code. `break` is usually used to force a loop or branch to exit when a certain condition is met, regardless of whether the loop condition is met.

Here's how the `break` statement is used:

Use `break` in `for` loops, `while` loops and `do...while` loops:

for (int i = 1; i <= 10; i++) {

    if (i == 5) {

        // When the condition is met, execute break and end the loop early

        break;

    }

    printf("Current i value: %d\n", i);

}

or:

while (condition) {

    // some code

    if (some_condition) {

        // When the condition is met, execute break and end the loop early

        break;

    }

    // more code

}

Using `break` in a `switch` statement:

switch (expression) {

    case 1:

        //Code for case 1

        break;

    case 2:

        //Code for case 2

        break;

    default:

        //default code

}

Here is an example demonstrating the use of `break`:
 

#include <stdio.h>

int main() {

    for (int i = 1; i <= 10; i++) {

        if (i == 5) {

            printf("It is found that i is equal to 5, end the loop early.\n");

            break; // End the loop early

        }

        printf("Current i value: %d\n", i);

    }

    return 0;

}

In this example, when the value of `i` is equal to 5, the `break` statement is executed, causing the loop to end prematurely.

The `break` statement is usually used to exit the loop immediately when a certain condition is met to save unnecessary iterations. In the `switch` statement, `break` is used to end the execution of a branch and jump out of the `switch` block to avoid executing other branches. Note that `break` will only exit the innermost loop or `switch` branch. If multiple loops or `switch` statements are nested, `break` will only exit the current loop or branch.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132913415