Learn C language in seven days-the third day (loop statement)

Insert image description here

1. Use a while statement loop to perform mathematical operations:

Using whilestatements, you can create a loop that repeatedly executes a block of code until a specified condition is no longer true.

Example 1: Find 2+4+6+···+100.

#include <stdio.h>

int main() {
    
    
    int n = 1, sum = 0, a = 2;
    while (n <= 50) {
    
    
        n = n + 1;
        sum = sum + a;
        a = a + 2;
    }
    printf("计算结果是 %d\n", sum);
    return 0;
}

This example whileloops through the sum of even numbers from 2 to 100.

operation result:
Insert image description here

Example 2: Find 2×4×6×8×···×50.

#include <stdio.h>

int main() {
    
    
    int n = 2;  // 初始值为2
    long long result = 1LL;  // 结果初始化为1LL

    while (n <= 50) {
    
    
        result *= n;  // 乘以当前的n
        n += 2;  // 递增n,跳到下一个偶数
    }

    printf("2×4×6×8×···×50 = %lld\n", result);

    return 0;
}

This example uses whilea loop to calculate the product of even numbers from 2 to 50.

operation result:

Insert image description here

Example 3: Find 1! + 2! + 3! + … + 20!.

#include <stdio.h>

int main() {
    
    
    int n = 1, sum = 0, a = 1;
    while (n <= 20) {
    
    
        n = n + 1;
        sum = sum + a;
        a = a * n;
    }
    printf("计算结果是 %d\n", sum);
    return 0;
}

This example uses whilea loop to calculate the sum of factorials from 1 to 20.

operation result:
Insert image description here

2. Use the while statement to loop:

whileLoops can also be used simply to execute a block of code repeatedly without necessarily requiring a counter or specific conditions.

Example 1: Output an integer between 100 and 300.

#include <stdio.h>

int main() {
    
    
    int n = 99;
    while (n < 300) {
    
    
        n = n + 1;
        printf("%d\t", n);
    }
    return 0;
}

This example uses whilea loop to output integers between 100 and 300.

operation result:
Insert image description here

3. Use the break statement to terminate the loop early:

breakstatement allows you to terminate the loop prematurely anywhere in the loop.

Example 1: Use a for loop to iterate i from 1 to 10. When i equals 5, we use the break statement to terminate the loop.

#include <stdio.h>

int main() {
    
    
    int i;
    
    for (i = 1; i <= 10; i++) {
    
    
        if (i == 5) {
    
    
            printf("遇到了 i 等于 5,终止循环\n");
            break;
        }
        printf("当前 i 的值是 %d\n", i);
    }

    return 0;
}

operation result:
Insert image description here

4. Use the continue statement to end this loop early:

continuestatement is used to end the current iteration of the current loop and immediately begin the next iteration.

Example 1: Output an integer between 100 and 300 that is not divisible by 4.

#include <stdio.h>

int main() {
    
    
    int n = 99;
    while (n < 300) {
    
    
        n = n + 1;
        if (n % 4 == 0)
            continue;
        printf("%d\t", n);
    }
    return 0;
}

This example uses whilea loop to output the integers between 100 and 300 that are not divisible by 4, and uses to continueend the situation that is divisible by 4 early.

operation result:
Insert image description here

5. Loop with do...while statement:

do...whileThe loop first executes the loop body and then checks whether the condition is met. Even if the condition is not met initially, the body of the loop will be executed at least once.

Example 1: Find 2+4+6+···+100.

#include <stdio.h>

int main() {
    
    
    int n = 1, sum = 0, a = 2;
    do {
    
    
        n = n + 1;
        sum = sum + a;
        a = a + 2;
    } while (n <= 50);
    printf("计算结果是 %d\n", sum);
    return 0;
}

This example uses do...whilea loop to calculate the sum of even numbers from 2 to 100.

operation result:
Insert image description here

6. Use for statement to loop:

forA loop is a compact loop structure usually used when the number of loops is known. It includes initialization, condition checks, and iteration statements.

Example 1: Find 1! + 2! + 3! + … + 20!.

#include <stdio.h>

int main() {
    
    
    int sum = 0, a = 1, n = 1;
    while (n <= 20) {
    
    
        a = n * a;
        sum = sum + a;
        n = n + 1;
    }
    printf("计算结果是 %d\n", sum);
    return 0;
}

This example uses whilea loop to calculate the sum of factorials from 1 to 20.

operation result:
Insert image description here

7. The difference between while statement, do...while statement and for statement:

whileLoops check conditions before entering the loop body and may not execute the loop body even once. do...whileThe loop first executes the loop body and then checks the condition, which will be executed at least once. forLoops provide a more compact loop structure and are typically used when the number of loops is known.

Insert image description here

Example:
Insert image description here

Guess you like

Origin blog.csdn.net/m0_53918860/article/details/132990974