π Mastering C Language Loop Structures (Part 1): A Complete Guide to For Loops
β Why Use Loops?
If we want to output “Hello” 5 timesπ
printf("δ½ ε₯½\n");
printf("δ½ ε₯½\n");
printf("δ½ ε₯½\n");
printf("δ½ ε₯½\n");
printf("δ½ ε₯½\n");
Too clumsy β Poor readability β Poor scalability β
β With loops:
β
Let the program automatically repeat a task, making the code more concise and intelligent!
β 1. Basic Syntax of For Loops
for(Initialization; Condition; Update) {
Loop statements;
}
π Execution OrderπInitialization β Condition Check β Execute β Update β Condition Check β β¦
| Part | Description |
|---|---|
| Initialization | Set the loop variable, executed once |
| Condition Check | Decides whether to continue the loop |
| Update | Executed after each loop |
π¦ Example 1: Print 1 to 5
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
printf("i = %d\n", i);
}
return 0;
}
Outputπ
i = 1
i = 2
i = 3
i = 4
i = 5
β Automatically increments β
π¦ Example 2: Calculate the Sum from 1 to 100
#include <stdio.h>
int main() {
int sum = 0;
for(int i = 1; i <= 100; i++) {
sum += i;
}
printf("Total = %d\n", sum);
return 0;
}
π The program enhances mathematical capability!π§
π¦ Example 3: User Input for Loop Count
#include <stdio.h>
int main() {
int n;
printf("Please enter the count:");
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
printf("This is the %d time execution\n", i);
}
return 0;
}
β Flexible and controllable β Commonly used for score entry, batch output
π₯ 4. Classic Case: Multiplication Table (Core Teaching Case)
#include <stdio.h>
int main() {
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= i; j++) {
printf("%dΓ%d=%-2d ", j, i, i*j);
}
printf("\n");
}
return 0;
}
Outputπ (Standard lower-left triangle multiplication table)
π Learning Points β Nested Loops β Inner loop handles single line content β Outer loop controls the number of lines
β‘ 5. Loop Control Statements (Must-Know)
<span>break</span> ββ Ends the entire loop
for(int i=1; i<=10; i++){
if(i == 5) break;
printf("%d\n", i);
}
Output:
1 2 3 4
<span>continue</span> ββ Skips this iteration of the loop
for(int i=1; i<=10; i++){
if(i == 5) continue;
printf("%d\n", i);
}
Output:
1 2 3 4 6 7 8 9 10
β Commonly used for filtering and data selection
β 6. Common Error Comparison Table
| Error Phenomenon | Cause | Correct Approach |
|---|---|---|
| Infinite loop stuck | Condition is always true | Update expression must be correct |
| Output one less | Boundary condition written incorrectly | <span><=</span> or <span><</span> must be clear |
| i++ written in the wrong position | Update expression missing | Write it in for() |
π Mnemonic:
β
Initialization must be correct, condition must be precise, and updates must not be omitted!
β 7. Classroom Exercises
All must be written as complete code!π
π Exercise 1
Loop output all even numbers from 1 to 20
π§© Hint:<span>i % 2 == 0</span>
π Exercise 2
Input 10 integers and calculate the average (keep 1 decimal place)
π§© Hint: First accumulate, then divide by 10
π Exercise 3 (Advanced)
Print the following pattern (asterisk triangle):
*
**
***
****
*****
π§© Hint: Nested for loops
π 8. Summary Review
| Learning Content | Mastery Level |
|---|---|
| For Loop Syntax Structure | β |
| Single Loop and Accumulation Logic | β |
| Nested Loops (Multiplication Table/Pattern Output) | β |
| Break and Continue | β |
| Error Warnings and Practical Experience | β |
π― You have mastered the basics of loops. In the next section, we will continue to improveπ
π Next Article Preview
β
Detailed Explanation of C Language While and Do-While Loops + Input Control Applications + Loop Termination Condition Checks
Includingπ β User keeps inputting until entering 0 β Loop password verification β Handling unknown number of inputs β Classic 7 practice questions
π IoT Smart Classroom
Persist a little every day, you will be stronger than yesterday πͺ Welcome to follow, letβs learn to code together!