Back to: C Tutorials For Beginners and Professionals
For Loop in C Language with Examples
In this article, I am going to discuss the For loop in C Language with Examples. Please read our previous articles, where we discussed the Do While loop in C Language with Examples. At the end of this article, you will understand what is for loop is and when and how to use for loop in the C program with examples.
For Loop in C Language:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. The for loop is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.
For loop contains 3 parts, such as
- Initialization
- Condition
- Iteration (increment or decrement statement)
Syntax to use for loop in C Language:
When we are working with for loop always execution process will start from the initialization block. After the initialization block, the control will pass to the condition block. If the condition is evaluated as true then control will pass to the statement block.
After execution of the statement block, the control will pass to the iteration block, from iteration it will pass back to the condition. Always repetition will happen beginning condition, statement block, and iteration only. The initialization block will be executed only once when we are entering the loop the first time.
When we are working with for loop everything is optional but mandatory to place 2 semicolons (;;). While we are working with for loop if the condition part is not given then it will repeat infinite times because the condition part will replace it as non-zero. So it is always true as like for(; 1; )
Whenever we are working with for loop it repeats in an anticlockwise direction. In for loop also the pre-checking process will occur i.e. before the execution of the statement block (body of the for loop), the condition part will be executed
Program to print the number from 1 to 10 using for loop
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d ", i); } }
Output: 1 2 3 4 5 6 7 8 9 10
Note: In implementation when we need to create more than one initialization, more than one iteration part then uses the comma as a separator for every expression. Whenever the body is not specified for the loop then automatically scope will be terminated to next (;) i.e. under the condition only 1 statement will be placed.
Program to enter a number and check whether that no is the perfect number or not?
If the sum of all factors is equal to that number then it is called a perfect number.
#include <stdio.h> int main() { int n, i, sum = 0; printf("\nenter a number "); scanf("%d", &n); for(i = 1; i<= n/2; i++) { if(n % i == 0) sum = sum + i; } if (sum == n && n != 0) printf("it is a perfect number"); else printf("it is not a perfect number"); return 0; }
Output:
Program to check whether a number is Armstrong no or not using C Language
If the sum of all individual cube values is equal to that number then it is called the Armstrong number.
#include <stdio.h> int main() { int n, rem, temp, sum = 0; printf("\nEnter a number : "); scanf("%d", &n); for(temp = n; temp != 0;) { rem = temp % 10; sum = sum + (rem* rem* rem); temp = temp /10; } if (sum == n && n != 0) printf("It is an Armstrong number"); else printf("It is not an Armstrong number"); return 0; }
Output:
Program to enter a number and check whether it is a prime number or not using for loop in C Language
The number which is divisible by 1 and itself is called a prime number.
#include <stdio.h> int main() { int n, i; printf("\nEnter a number : "); scanf("%d", &n); for(i = 2; i < n; i++) { if(n % i == 0) break; } if (i == n && n >= 2) printf("It is a prime number"); else printf("It is not a prime number"); return 0; }
Output:
Program to print the Fibonacci series up to a given number using for loop in C Language
#include <stdio.h> int main() { int n, n1 = 0, n2 = 1, temp; printf("\nEnter a number : "); scanf("%d", &n); if(n >= 1) { printf("%d %d ", n1, n2); temp = n1 + n2; for(; temp <= n;) { printf("%d ", temp); n1 = n2; n2 = temp; temp = n1+n2; } } else printf("please enter a number greater than zero"); }
Output:
Nested for Loop in C Language:
In a nested for loop, one or more statements can be included in the body of the loop. In a nested for loop, the number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop. The syntax to use nested for loop is given below.
Program to understand nested for loop in C Language:
#include <stdio.h> int main() { int i; //for outer loop counter int j; //for inner loop counter for( i=1; i<=5; i++) { for( j=1; j<=10; j++) { printf("%d ",j); } printf("\n"); } return 0; }
Output:
In the next article, I am going to discuss Break Statements in C Language with examples. Here, in this article, I try to explain For Loop in C Language with examples. I hope you enjoy this For Loop in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.