ITDeveloper : C Programming
C Programming
Step by Step Tutorials
![]() Share with a Friend |
for Loop in C
The for loop in C Language provides a functionality/feature to repeat a set of statements a defined number of times. The for loop is in itself a form of an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the initialization, condition, and updating statements as part of its syntax. It is mainly used to traverse arrays, vectors, and other data structures.
Syntax of for Loop
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}
The working of the for loop
- The initialization statement is executed only once.
- Then, the test expression is evaluated. If the test expression is evaluated to false, the forloop is terminated.
- However, if the test expression is evaluated to true, statements inside the body of the forloop are executed, and the update expression is updated.
- Again the test expression is evaluated.
This process goes on until the test expression is false. When the test expression is false, the loop terminates.
for loop Flowchart
Example 1: for loop
// Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
Explanation:
- i is initialized to 1.
- The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for loop is executed. This will print the 1 (value of i) on the screen.
- The update statement ++i is executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body of for loop is executed. This will print 2 (value of i) on the screen.
- Again, the update statement ++i is executed and the test expression i < 11 is evaluated. This process goes on until i becomes 11.
- When i becomes 11, i < 11 will be false, and the for loop terminates.
Example 2: for loop
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
OUTPUT:
Enter a positive integer: 15
Sum = 120
Explanation:
The value entered by the user is stored in the variable num. Suppose, the user entered 10.
The count is initialized to 1 and the test expression is evaluated. Since the test expression count<=num (1 less than or equal to 10) is true, the body of for loop is executed and the value of sum will equal to 1.
Then, the update statement ++count is executed and count will equal to 2. Again, the test expression is evaluated. Since 2 is also less than 10, the test expression is evaluated to true and the body of the for loop is executed. Now, sum will equal 3.
This process goes on and the sum is calculated until the count reaches 11.
When the count is 11, the test expression is evaluated to 0 (false), and the loop terminates.
Then, the value of sum is printed on the screen.
Example 3:
This example will only print even values between 0 and 10
// Print even numbers between 0 to 10
#include <stdio.h>
int main() {
int i;
for (i = 0; i <= 10; i=i+2)
{
printf("%d ", i);
}
return 0;
}
OUTPUT:
0 2 4 6 8 10