For Loop in c

For Loop in C Programming

The for loop is a repetition control structure of a looping control system, that allows you to efficiently write any loop that needs to execute a specific number of times. Like other looping control, the ‘for’ loop statement is very important and useful while programming in C language. In most of the looping situations, for-loop is generally used as it is very effective and it helps execute a block of statements for several numbers of times. The for-loop body can be a single or block of statements.

The general form of the for-loop structure is it has three expressions. Each expression is separated by semicolons. As compared to the while-loop, the for-loop is a little bit different in the structure form. In the while-loop case, the expression1 that is counter variable is initialized outside the while-loop, the expression2 is a condition that is checked first if it satisfies then the control goes inside the while block to execute the statements followed by updating the third expression.t this process goes on until the condition is true. But, in for-loop, the counter variable can be initialized before the loop starts and at the time of running the loop as well. The update expression is done anywhere in the loop-body.

Syntax of For Loop In C Programming

for(expression1; expression2; expression3)
{
    Statement1;
    Statement2;
    Statement n;
}

or in a better way of understanding you can see the below Syntax

for(init; condition; increment) 
{
   statement(s);
}

Here, the expression1(counter variable) is the initialization process, expression2 is testing condition expression and expression3 is updating an expression. The counter variable is executed only once when the loop starts and used to initialize the loop variables. This expression is an assignment expression. Expression2 is a condition that is tested before each iteration of the loop. The condition is generally in the form of relational expression and uses logical operators. Expression3 is an update expression that is very important for iterating the loop. The update expression is executed after the body of the loop is executed.

Flow Chart Image need to be added

Here, first, the loop variable is initialized first and then the condition is checked if the condition is true the control executes the statements sequentially. After full execution of the statements, the update expression is executed. It modifies the counter variable and then again, the condition is checked, and if it is true, the body of the loop is executed. So, this process propagates until the condition is true.

For Loop Example: print the natural numbers ranging from one to five using for-loop

#include<stdio.h>
#include<conio.h>
int main ()
{                                 
int i;
printf(“The first five natural numbers are : ”);
for(i=1;i<=5;i++)
{
Printf(“%d \n”,i);
}
}
When the above code executed, it produces the below results:
Output:
the first five natural numbers are:
1
2
3
4
5

Nesting of for-loop in C Programming

It means there are one or more for-loops in another for-loop. This concept is very much important in a programming language as it is very helpful in storing data in a systematic manner. In a nested for-loop, the innermost for-loop is executed first. After execution, the immediate outer for-loop of the inner for-loop is executed sequentially. The whole process goes on until the condition of the outermost for-loop is true.

Syntax of Nested For Loop in C Programming

for(expression1; expression2;expression3)
{
for(expression1; expression2;expression3)
{
Statement1;
Statement2;
}
}

Nested For Loop Flow Chart

Nested For Loop Flow Chart

In nested for loop, there will be multiple for-loops. Each for loop will be inside another for-loop. When this program execution will start, it will start from up-down as per the c-compiler design. you can add unlimited for loop in a nested format. But it takes more time in execution in branching code execution. So basically 2-3 for-loop can be made in nested format for better performance. The developer should be smart to implement any looping method to take care of code size and execution time.

Example: program to print the pyramid of asterisk

#include<stdio.h>
#include<conio.h>
int main()
 {   
int i,j,n=4;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“* ”);                                          
}                               
printf(“\n”);
}
}

Output:
       *
       **
       ***
       ****

Break statement in for-loop

If the programmer wants to halt the ongoing for-loop execution before control reaches the condition to be false. He can achieve it with the help of the keyword called break statement. The break statement is used with some conditions in for-loop with respect to avoiding the sudden halt of the execution of the for-loop body. As long as the condition applied to break statement is not met. Till then execution will go. Once the break statement condition is met. The control comes out of the body of the for-loop.

For Loop Syntax with Brake Statement

for(expression1; expression2;expression3)
{
if(condition)
{
Break;
}
}

Example: check if a number is a prime number or not

#include<stdio.h>
#include<conio.h>
int main ()
{            
int i,n,flag=1;
printf(“Hi PiEmbSysTech Please enter the number \n”);
for (i=2; i<=n/2;i++)
{         
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==0)
{
printf (“it is not a prime number \n”);
}
else
{
printf (“it is a prime number \n”);
}
}

Output:

Enter the number:
11
It is a prime number

Here, the input is given as n=11. In the for-loop, all the possible factors except 1 are checked. the factor comes true as the factor of 11 then the break statement is executed. Finally, the result is it is a prime number.

For-loop plays a vital role in making formation of matrix:

As we are aware that the matrix has both row and Column. In this situation, other looping structures are not fully efficient for making matrix formation as they sometimes give unexpected results. The nested for-loop is much efficient for this task. The outermost for-loop is used for rows and the inner for-loop is used for the column.

Example: print the matrix formation
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
For(j=1;j<=4;j++)
{
Printf(“%d”,i);
}
Printf(“\n”);
}
}
Output:

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

Here, the outer for-loop is used to draw the rows for the matrix. it executes till the condition is true. The inner for-loop is used to execute columns of the matrix. Here also condition is applied to halt the execution. After the inner for-loop execution, the control goes back to the outer for-loop to draw the next row. Like this, it goes on till the outer condition is true.

Passionate to write computer program

This article is written by Pitabash Choudhury. He is pursuing his MCA in REC. He likes to write the programs in a way of next-generation programs to develop a lightweight & powerful compiler.”

Er. Pitabash Choudhury

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
Scroll to Top