Loop statements in C language are used to repeatedly execute a block of code.
- If the number of iterations is known, the for loop is typically used.
- If the number of iterations is unknown but the loop condition is known before the loop starts, the while loop is used.
- If the loop body must execute at least once, the do-while loop is used.
1. for loop
for (initialization; condition; update) { // loop body}
#include <stdio.h>
int main(int argc, char *argv[]) { int i = 0; int j = 0; // Print numbers from 1 to 10 // i starts from 1, if i <= 10, execute the statements inside, otherwise exit // After each execution, i++ for (i = 1; i <= 10; i++) { // No newline added here, so each print is on the same line printf("%d ", i); } /* This can also be written as: for (i = 1; i <= 10;) { printf("%d ", i); i++; } */ printf("\n"); // Nested for loop - Print multiplication table for (i = 1; i <= 9; i++) { for (j = 1; j <= i; j++) { /* To output neatly %-2d means decimal output, length 2, left-aligned If not enough, fill with spaces, if too much, output actual length For example, if i*j result is 4, then %-2d outputs "4 " If result is 16, then output is "16" */ printf("%d×%d=%-2d ", j, i, i*j); } printf("\n"); } return 0;}
Output:
1 2 3 4 5 6 7 8 9 10
1×1=1
1×2=2 2×2=4
1×3=3 2×3=6 3×3=9
1×4=4 2×4=8 3×4=12 4×4=16
1×5=5 2×5=10 3×5=15 4×5=20 5×5=25
1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
2. while loop
while (condition) { // loop body}
#include <stdio.h>
int main(int argc, char *argv[]) { int num = -1; int sum = 0;
while (num != 0) { printf("Enter a number (input 0 to end): "); scanf("%d", &num); sum += num; } printf("Total: %d\n", sum);
return 0;}
Output:
Enter a number (input 0 to end): 12
Enter a number (input 0 to end): 13
Enter a number (input 0 to end): 0
Total: 25
3. do-while loopExecutes first, then checks the condition, so it will execute at least once.
do { // loop body} while (condition);
#include <stdio.h>
int main(int argc, char *argv[]) { int num = 20; // Executes at least once do { printf("num=%d\n", num); } while (num <= 0); return 0;}
Output:
num=20
4. break and continue loop control
#include <stdio.h>
int main(int argc, char *argv[]) { int i = 0; for (i = 1; i <= 20; i++) { if (i == 12) { // break means to end the loop directly break; }
if (i % 2 != 0) { // continue means to skip the rest of the loop and continue continue; // Skip odd numbers } printf("%d ", i); } printf("\n"); return 0;}
Output:
2 4 6 8 10
5. Infinite loopAlso known as a dead loop, it is a loop that keeps executing without an exit condition.
#include <stdio.h>// The sleep function is declared in unistd.h#include <unistd.h>
int main(int argc, char *argv[]) { /* This is very CPU intensive, if your CPU has 8 cores Running this program 8 times will make the CPU almost 100% The system will be basically frozen */ while (1) { printf("Hello, World!\n"); // Try to avoid CPU-intensive infinite loops // If necessary, you can add a sleep // sleep(1); } return 0;}
Infinite loops are often used in real-world applications, especially in multithreaded programming.