Skip to content

For Loop's Guide in C Language

I. Introduction

In C language, the for loop statement is a commonly used looping statement. This article mainly introduces the basic syntax structure of the for loop, the use of continue statements and break statements within for loops, and the three variants of for loops.

II. Basic Syntax Structure of for Loop

for (expression1; expression2; expression3) { loop statement; }

Expression1: The initialization part, used to initialize the loop variable.
Expression2: The condition judgment part, used to determine when the loop should terminate.
Expression3: The adjustment part, used to adjust the loop condition.

Case: Please use a for loop to output all integers from 1 to 1

Analysis The problem requires outputting all integers from 1 to 10. Since they are integers, we need to define an int type loop variable, which will hold the value to be output. The first value to output is 1, so we can initialize the loop variable to 1. The largest value to output is 10, so the loop condition should be that the value of the loop variable is less than or equal to 10. After outputting 1, we output 2, so the value of the loop variable should be incremented by one each time. The loop statement is simple, just output the value of the loop variable. Example Code

c
#include <stdio.h>
    int main()
    {
     int i = 0;
     for (i = 1; i <= 10; i++)
     {
      printf("%d ", i);
     }
     return 0;
    }

III. The Use of break and continue in for Loops

1. The break statement is used to permanently terminate a loop

In a for loop statement, if a break statement is used, it will immediately terminate all remaining loop statements and exit the loop.

Case: Output all integers from 1 to 10, but stop after outputting the fourth number

Analysis The problem requires outputting all integers from 1 to 10, so we can use the previous code. However, the problem also requires that we stop outputting after the fourth number. Therefore, after we output the fourth number (when the loop variable's value is 4), we use the break statement to terminate the loop. Example Code

c
   #include <stdio.h>
   int main()
   {
    int i = 0;
    for (i = 1; i < 11; i++)
    {
     printf("%d ", i);
     if (i == 4)
     {
      break;
     }
    }
    return 0;
   }

2. The continue statement is used to skip the remaining part of the current loop iteration

In a for loop, the continue statement is used to skip the remaining statements of the current loop iteration and return to expression3 to adjust the loop variable. Example Code

c
#include <stdio.h>
    int main()
    {
     int i = 0;
     for (i = 1; i < 11; i++)
     {
      if (i == 5)
      {
       continue;
      }
      printf("%d ", i);
     }
     return 0;
    }

IV. Variants of the for Loop

1. for Loop without a Loop Condition

In the for loop, all three expressions within the parentheses can be omitted. If no loop condition is set, the default loop condition is always true, and the program will enter an infinite loop. Example Code

c
#include <stdio.h>
    int main()
    {
     int i = 0;
     for (i = 1; ; i++)
     {
      printf("haha! ");
     }
     return 0;
    }

2. Nested for Loops

A for loop can contain another for loop within its block. When the condition of the outer for loop is true, if the condition of the inner for loop is also true, the inner for loop will be executed. After the inner for loop finishes, control returns to the outer for loop, and if its condition is still true, it will enter the inner for loop again. This process continues until the condition of the outer for loop is no longer true. Example Code (Multiplication Table)

c
#include <stdio.h>
    int main()
    {
     int i, j;
     int sum = 0;
     for (i = 1; i < 10; i++)
     {
      for (j = 1; j < i + 1; j++)
      {
       sum = i * j;
       printf("%d * %d = %d", i, j, sum);
       if (sum > 9)
       {
        printf("  ");
       }
       else {
        printf("   ");
       }
     
       if (i == j)
       {
        printf("\n");
       }
      }
     }
     return 0;
    }

3. Multiple Loop Variable Expressions

The for loop supports having multiple loop variables in the three expressions within the parentheses. Example Code

c
#include <stdio.h>
    int main()
    {
     int i, j;
     for (i = 0, j = 0; i < 3 && j < 5; i++, j++)
     {
      printf("good!\n");
     }
     return 0;
    }

Analysis In this code, two loop variables are defined.The loop condition is controlled by these two variables. && represents a logical AND, meaning both conditions on the left and right must be true for the loop condition to be satisfied. || represents a logical OR, meaning if either condition on the left or right is true, the loop condition is satisfied. "good!" is printed in each iteration of the for loop. When the third iteration of the for loop ends, the loop variable i is adjusted to 3.And the loop condition is no longer met, causing the loop to terminate.