Mastering C Language Loop Structures (Part 1): A Complete Guide to For Loops

πŸ” 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!

Leave a Comment