For Loop in C Language [ Full Information With Examples
Hello friends, today we are going to learn about what is For loop. And how do we use For loop in C language?
Earlier, we had learned in detail about the While loop and Do While loop. If you have not read that article, then you will definitely read it once. So that you will understand this article very soon.
For Loop in C
For Loop is an entry-controlled loop In which the condition inside For Loop is checked before executing the statement, And if the condition is true only then the statement inside For Loop is executed or run.
In C language, For Loop is used to execute a particular statement or block repeatedly until a particular condition is true.
In For Loop, we write both the initialization and control condition of the variable together inside the parentheses “()”. If the condition of For Loop is true, then the statement inside it is run or else the statement is not run.
Syntax of For Loop in C
for (initialization; condition test; update expression)
{
// body of the loop
// Statements to be executed repeatedly
}
Initialization -: Here we initialize or store a value in a variable. Initialization is performed only once in the for loop. example: int i = 1;
Condition Test: – The condition is written in this section every time we have to run a loop. The condition can be any Boolean expression. For example: i <= 10;
Once the loop is run, the condition is checked again and if the condition returns true this time, then the loop runs again and if the condition is false then the control comes out of the loop.
Update Expression -: Here the increment or decrement operator is used to update the Expression. for example: i ++;
Let’s understand the for loop better by this one example.
Example of For Loop
Here we are going to make a program to print “Hello World” 10 times with the help of For loop.
// C program to illustrate for loop
#include <stdio.h>
int main ()
{
int i;
// i = 1; Initialization
// i<= 10 Condition Test
// i++ Update Expression
for (i = 1; i<=10; i++)
{
printf ("Hello World \n");
}
return 0;
}
Let’s see how this program will execute?
How Program Work?
- The first variable will be the declare.
- The control goes to the for loop and store 1 value in the i variable
- After that the condition i <= 10 will be checked.
- The stipulated condition is correct, so the statement inside the for loop will execute once. Which will print once Hello World on the screen.
- When the statement of the body of the for loop is executed once, then the control will go to the Update Expression where the value of i will be incremented due to i ++ being written.
- After incrementing the value of i, the control will again go to section i <= 10 in the condition of the for loop and check the condition again.
- If this time the condition is true, then the statements inside the for loop will be executed again and the control will again go to Update Express.
- Where the value of i will be incremented again due to i ++ being written.
- After that the control will again go to section i <= 10 for the condition of the for loop and check the condition again.
- Here the action will continue until the condition with i <= 10 becomes false.
- When the condition with i <= 10 is checked 11 times, then the condition will be wrong because by then the value of i will be 11 so the control loop will be out.
- Before coming out of the control loop, “Hello World” must have been printed 10 times.
The output of this program will be
Output -:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Nested For Loop in C
When we use one or more for loops or any other loop inside the For loop, then we call it Nested For Loop.
Syntax of Nested For loop
for (initialization; condition; update Expression)
{
for (initialization; condition; update Expression)
{
// inner For loop statements.
}
// outer For loop statements.
}
Example
Below we are going to create a program in which we will print all the prime numbers from 2 to 100.
#include <stdio.h>
int main ()
{
int i, j; /* local variable definition */
for (i = 2; i<100; i++)
{
for (j = 2; j<= (i/j); j++)
{
if (!(i%j)) break; // if factor found, not prime
}
if (j> (i/j)) printf ("%d is prime \n", i);
}
return 0;
}
Output -:
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
Read More -:
- While and Do while Loop in C
- For Loop in C
- Goto Statement in C
- Break Statement in C
- Switch Statement in C
- Control Statements in C
- Continue statement in C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, hope that after reading this post today, you have learned what is the For loop in C language And how to use it in C language.
Friends, I hope you have found the answer of your question and you will not have to search for the For loop in C language.
However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the For loop in C Language.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.