1. Use a while statement loop to perform mathematical operations:
Using while
statements, 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 while
loops through the sum of even numbers from 2 to 100.
operation result:
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 while
a loop to calculate the product of even numbers from 2 to 50.
operation result:
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 while
a loop to calculate the sum of factorials from 1 to 20.
operation result:
2. Use the while statement to loop:
while
Loops 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 while
a loop to output integers between 100 and 300.
operation result:
3. Use the break statement to terminate the loop early:
break
statement 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:
4. Use the continue statement to end this loop early:
continue
statement 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 while
a loop to output the integers between 100 and 300 that are not divisible by 4, and uses to continue
end the situation that is divisible by 4 early.
operation result:
5. Loop with do...while statement:
do...while
The 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...while
a loop to calculate the sum of even numbers from 2 to 100.
operation result:
6. Use for statement to loop:
for
A 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 while
a loop to calculate the sum of factorials from 1 to 20.
operation result:
7. The difference between while statement, do...while statement and for statement:
while
Loops check conditions before entering the loop body and may not execute the loop body even once. do...while
The loop first executes the loop body and then checks the condition, which will be executed at least once. for
Loops provide a more compact loop structure and are typically used when the number of loops is known.
Example: