How To Best Implement For Loop In C?

Published on Sep 10,2019 5 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

In this article on For Loop in C we will explore everything about For loops right from the basic syntax to different ways implementing it. Following pointers will be covered in this article,

So let us get started then,

For Loop in C

Loop is one of the fundamental concepts in all programming languages as it simplifies complex problems. In simple words, loop repeats the same set of code multiple times until the given condition returns false. So, instead of writing the same code again & again, we can use loop to execute the same code multiple times.

For example, to print natural numbers from 1 to 100, either you can write 100 print statements, or you can run loop for 100 iterations and print the natural numbers. Obviously the second option is easier & more feasible. 

Moving on with this For Loop In C article,

Loops in C

Loop consists of two parts:

  • Body of Loop: consists of a set of statements that need to be continuously executed
  • Conditional Statement: is a condition. If it is true, then the next iteration is executed else the execution flow exits the loop.

Types of Loop in C

There are two types of loops in C i.e. entry-controlled loops & exit controlled loops.

  • Entry-controlled loops: Entry controlled loops are those loops where the test condition is tested before executing the body of a loop. For & While loop are entry-controlled loops.
  • Exit controlled loops: Exit controlled loops are those loops where the test condition is tested after executing the body of a loop. do-while loop is an exit-controlled loop.

Moving on with this For Loop In C article,

For Loop Syntax

For Loop is a looping structure that is used to execute a sequence of code until the given condition returns false. The best condition to use for loop is when the number of iterations is known in advance.

Syntax:

for(initialization; condition test; increment or decrement)
{
//block of code to be executed repeatedly
}

Flow Diagram of For Loop

Loop - For Loop In C - Edureka

Step 1: In the execution flow, first the counter variable gets initialized.

Step 2: The test condition is verified, where the counter variable is tested for a given condition. If condition returns true then the block of code residing inside the function body gets executed, else the for loop gets terminated & control comes out of the loop.

Step 3: In case of successful execution of the function body, the counter variable gets incremented or decremented based on the operation.

Example

#include <stdio.h>
int main()
{
int counter;
for (counter =1; counter<=10; counter++)
{
printf("%dn", counter);
}
return 0;
}

Output:

Output - For Loop In C - Edureka

Moving on with this For Loop In C article,

Different Forms of For Loop In C

  • Counter++ & counter+1 yields the same output.

Example:

#include <stdio.h>
int main()
{
int counter;
for (counter =1; counter<=10; counter=counter+1)
{
printf("%dn", counter);
}
return 0;
}

Output:

Output - For Loop In C - Edureka

You can skip the initialization of the counter variable & it can be declared before the loop.

Example:

#include <stdio.h>
int main()
{
int counter=1;
for(; counter<=10; counter=counter+1)
{
printf("%dn", counter);
}
return 0;
}

Output:

Output - For Loop In C - Edureka

You can skip the initialization of the counter variable, but the semicolon before the test condition should be present, otherwise it will throw compilation error.

You can also skip the incrementing or decrementing of the counter. But in this scenario the counter should be incremented inside the for-loop body. 

Example:

#include <stdio.h>
int main()
{
int counter;
for (counter=1; counter<=10;)
{
printf("%dn", counter);
counter=counter+1
}
return 0;
}

Moving on with this For Loop In C article,

You can skip the condition in the for loop, which will result in an infinite loop.  

Example:

#include <stdio.h>
int main()
{
int counter;
for (counter=1; ; counter++)
{
printf("%dn", counter);
}
return 0;
}

Output:

Infinte Loop

We can initialize more than one variable in for loop.

Example:

#include <stdio.h>
int main()
{
int x, y, z;
for (x=1, y=2, z=3; x<5 ; x++, y++, z++)
{
printf("x %dn", x);
printf("y %dn", y);
printf("z %dn", z);
}
return 0;
}

Output:

Output - For Loop In C - Edureka

Moving on with this For Loop In C article,

Nested for loop in C

You can put one for loop inside another for loop in C. This is called nested for loop. 

Example:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, k, rows, blank;
printf("Enter the number of rows:");
scanf("%d",&rows);
blank = rows;
for ( i = 1 ; i <= rows ; i++ )
{
for ( k = 1 ; k < blank ; k++ )
printf(" ");
blank--;
for ( k = 1 ; k <= 2*i - 1 ; k++ )
printf("*");
printf("n");
}
return 0;
}

Example:

Output - For Loop In C - Edureka

Moving on with this For Loop In C article,

Jumping Out of Loops

In various scenarios, you need to either exit the loop or skip an iteration of loop when certain condition is met. So, in those scenarios are known as jumping out of the loop. There are two ways in which you can achieve the same. 

break statement

When break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. 

In case of nested loop, if the break statement is encountered in the inner loop then inner loop is exited.

Example:

#include <stdio.h>
int main()
{
int counter;
for (counter=1; counter<=10; counter++)
{
if(counter==5)
{
break;
}
printf("%dn", counter);
}
return 0;
}

Output:

Output - For Loop In C - Edureka

Continue statement

Continue Statement sends the control directly to the test-condition and then continue the loop process. 

On encountering continue keyword, execution flow leaves the current iteration of loop, and starts with the next iteration.

Example:

#include <stdio.h>
int main()
{
int counter;
for (counter =1; counter<=10; counter++)
{
if(counter%2==1)
{
continue;
}
printf("%dn", counter);
}
return 0;
}

Output:

Output - For Loop In C - Edureka

With this we come to the end of this blog on ‘For Loop In C’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.Implement the above code with different strings and modifications. Now, we have a good understanding of all key concepts related to the pointer.

Got a question for us? Mention them in the comments section of  this blog and we will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.