• Home
  • 2.14 for Loop

2.14 for Loop

View Categories

2.14 for Loop

< 1 min read

Table of Contents

The for loop combines initialization, condition checking, and iteration in a single loop statement. It is commonly used when the number of iterations is controlled by a counter.

#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i <= 5; i++)
    {
        printf("%d\n", i);
    }

    return 0;
}

Example Output #

1
2
3
4
5

Explanation #

The for loop contains three expressions:

for (i = 1; i <= 5; i++)
  • i = 1 initializes the loop counter.
  • i <= 5 is the condition checked before each iteration.
  • i++ increments the counter after each iteration.

The execution order is:

  1. i is initialized to 1.
  2. The condition i <= 5 is checked.
  3. The loop body executes.
  4. i++ increments i.
  5. Steps 2–4 repeat until the condition becomes false.

When i becomes 6, the condition evaluates to 0, so the loop terminates.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *