The for loop is used to repeat a block of code multiple times. It is especially useful when you know, or can express, the loop control in terms of an initialization, a condition, and an update.
for loop has three main parts:
initialization, condition, and
update. The loop continues while the condition is true.
A for loop repeatedly executes a block of statements while
its condition remains true.
for (initialization; condition; update)
{
// statements
}
The loop is commonly used for counting, processing arrays, generating sequences, and repeating tasks.
for (initialization; condition; update)
{
statement;
}
The three expressions are separated by semicolons.
for (int i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
Output:
1
2
3
4
5
The loop starts at 1 and continues until i becomes greater
than 5.
Initialization usually sets the starting value of the loop variable.
for (int i = 1; i <= 5; i++)
{
printf("%d ", i);
}
Here, int i = 1 initializes the loop variable to 1.
The condition determines whether the loop body should execute.
for (int i = 1; i <= 5; i++)
{
printf("%d ", i);
}
The body executes while i <= 5 is true.
The update expression changes the loop variable after each iteration.
for (int i = 1; i <= 5; i++)
{
printf("%d ", i);
}
The i++ expression increases i by one after each
iteration.
The basic execution order is:
for (int i = 1; i <= 10; i++)
{
printf("%d\n", i);
}
This loop prints the numbers from 1 through 10.
for (int i = 10; i >= 1; i--)
{
printf("%d\n", i);
}
The decrement operator -- decreases the value by one after
each iteration.
for (int i = 2; i <= 10; i += 2)
{
printf("%d ", i);
}
Output:
2 4 6 8 10
The update expression increases the value by 2 each time.
for (int i = 1; i <= 10; i += 2)
{
printf("%d ", i);
}
Output:
1 3 5 7 9
int sum = 0;
for (int i = 1; i <= 5; i++)
{
sum = sum + i;
}
printf("Sum = %d", sum);
Output:
Sum = 15
The variable sum stores the running total.
int number = 5;
for (int i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", number, i, number * i);
}
This program generates the multiplication table of 5.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
printf("%d ", i);
}
return 0;
}
The number entered by the user determines how many numbers are printed.
int number = 5;
int factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial = factorial * i;
}
printf("Factorial = %d", factorial);
Output:
Factorial = 120
for (int i = 0; i <= 20; i += 5)
{
printf("%d ", i);
}
Output:
0 5 10 15 20
The update expression can increase the loop variable by any suitable amount.
for (int i = 10; i >= 0; i--)
{
printf("%d ", i);
}
Output:
10 9 8 7 6 5 4 3 2 1 0
If a loop contains only one statement, braces may technically be omitted.
for (int i = 1; i <= 5; i++)
printf("%d ", i);
However, braces are recommended when a loop contains multiple statements or when future modifications are likely.
for (int i = 1; i <= 3; i++)
{
printf("Number = %d\n", i);
printf("Hello\n");
}
Both statements execute during every iteration.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
}
}
An if statement can be used inside a loop to process only
selected values.
A for loop can be placed inside another for loop.
This is called a nested loop.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
printf("%d %d\n", i, j);
}
}
The inner loop completes its iterations for each iteration of the outer loop.
A for loop can be written without a condition:
for (;;)
{
printf("Running...\n");
}
This creates an infinite loop because there is no condition that becomes
false. Such a loop should normally contain a controlled way to terminate,
such as a break statement.
The initialization, condition, and update expressions can be omitted, but the semicolons remain necessary.
int i = 1;
for (; i <= 5; )
{
printf("%d ", i);
i++;
}
This works, although the normal three-part form is usually easier to read for simple counting loops.
A wrong condition can make a loop execute too many or too few times.
for (int i = 1; i < 5; i++)
{
printf("%d ", i);
}
Output:
1 2 3 4
If you want to include 5, use i <= 5.
If a loop variable never changes and the condition remains true, the loop can become infinite.
int i = 1;
for (;;)
{
printf("%d ", i);
}
Here, i never changes and there is no stopping condition.
A normal counting loop should update its control variable:
for (int i = 1; i <= 5; i++)
{
printf("%d ", i);
}
An unnecessary semicolon after the for statement creates an
empty loop body.
for (int i = 1; i <= 5; i++);
printf("Hello");
The loop above does not repeat the printf() statement.
The semicolon is the loop body.
Correct:
for (int i = 1; i <= 5; i++)
{
printf("Hello\n");
}
#include <stdio.h>
int main()
{
int n;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
This program calculates the sum of all integers from 1 to the number entered by the user.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
for (int i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n",
number, i, number * i);
}
return 0;
}
#include <stdio.h>
int main()
{
int number;
int factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0)
{
printf("Factorial is not defined for negative integers.");
}
else
{
for (int i = 1; i <= number; i++)
{
factorial *= i;
}
printf("Factorial = %d", factorial);
}
return 0;
}
For larger values, an int can overflow, so appropriate data
types and input limits should be considered.
Practice the following programs:
Practice changing the initialization, condition, and update parts to understand how each part controls the loop.
for loop is used to repeat a block of code.for loop can count upward or downward.if statements and nested loops.Question: Which part of a for loop changes the loop variable after each iteration?