for loop in C programming with examples (2022)

for loop in C programming with examples (2022)

Imagine you have created a coding community where you need to take member details as input and store them. you cannot write a separate line of code for each member but, you can repeat the same line for each member. this process is called looping. Let us see how to create and use for loop in C.

Introduction

There are three types of loops in C language. while, do while, and for loop each having its own advantages. Let us learn their syntaxes and know when to use them.

General Characteristics of a Loop

A Loop always requires three conditions. Those are initialization, Increment, and termination conditions. We will talk about these in the upcoming section.

Types of Loops

The three types of loops are:

  • While Loop
  • Do While Loop
  • For Loop

While Loop

A while loop is used when you don’t know how many times a loop should run, In other words, Termination Condition.

Syntax

//Initialisation Condition
i=0;
while(Termination Condition)
{
//Statements
//Increment Condition
i++;
}Code language: C/AL (cal)

Illustration

Let us print the first 11 to 20 natural numbers using a while loop.

#include <stdio.h>
int main() {
    //Initialisation Condition
    int per=20;
    //Termination Condition in Paranthesis
    while(per>10)
    {
        printf("%d\n",per);
        //Increment or Decrement
        per--;
    }
return 0;
}
Code language: C/AL (cal)

output

20
19
18
17
16
15
14
13
12
11

Do While Loop

The main usage of do-while loops occurs in menu-driven programs where we execute the loop at least once to get the input based on the menu displayed.

Syntax

//Initialising
i=0;
do
{
//Statements
//Increment or Decrement
i++;
}
while(Termination Condition);Code language: C/AL (cal)

Illustration

Let us see the same example as above so that we have a better understanding.

#include <stdio.h>
int main() {
    //Initialisation Condition
    int per=20;
    //Termination Condition in Paranthesis
    do
    {
        printf("%d\n",per);
        //Increment or Decrement
        per--;
    }
    while(per>10);
return 0;
}
Code language: C/AL (cal)

output

20
19
18
17
16
15
14
13
12
11

For Loop

In for loop, we declare all the required conditions in a single parenthesis. This loop is mainly used when we know all conditions beforehand.

Syntax

for(initialise;Termination;Increment)
{
//Statements
}Code language: C/AL (cal)

Illustration

#include <stdio.h>
int main() {
for(int i = 11;i<=20;i++)
{
    printf("%d\n",i);
}
return 0;
}Code language: C/AL (cal)

output

11
12
13
14
15
16
17
18
19
20

Nesting of Loops

The process of writing a looping statement under another looping statement is called a Nested loop.

Illustration

Suppose you have to print the first 5 multiples of 1 to 3. By using a nested loop we can solve this illustration.

#include <stdio.h>
int main() {
for(int i = 1;i<=3;i++)
{

//Nesting a loop
for(int j = 1;j<=5;j++)
{
    printf("%d*%d=%d\n",i,j,i*j);
}
}
return 0;
}Code language: C/AL (cal)

output

1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15

Controlling a Loop

Usually, loops can be controlled for some specific values using loop control statements.

There are two types of loop control statements. They are:

  • Break
  • Continue

Usage of Break Statement

Break is a pre-defined keyword in C language. whenever it is used in a loop, the loop generally stops at that iteration.

Syntax

break;Code language: JavaScript (javascript)

Illustration

Let us assume that the main objective of this example is to print 11 to 20 numbers except the loop has to stop if a 5 multiple occurs.

#include <stdio.h>
int main() {
for(int i = 11;i<=20;i++)
{
  if(i==15) 
  break;
  else
  printf("%d",i);
}
return 0;
}Code language: C/AL (cal)

Output

11
12
13
14

If you observe carefully the loop stops at 14 as 15 is a multiple of 5.

Now that we have understood Break let us see continue.

Usage of Continue Statement

Continue is a pre-defined keyword that is used to skip the loop at a particular value.

The main difference between break and continue is that break stops the loop while continue skips the loop.

Syntax

continue;Code language: JavaScript (javascript)

Illustration

Consider the same example as above but this time we skip printing the multiples of 5 between 11 to 20.

#include <stdio.h>
int main() {
for(int i = 11;i<=20;i++)
{
    if(i%5==0)
    continue;
    printf("%d\n",i);
    
}
return 0;
}Code language: C/AL (cal)

Output

11
12
13
14
16
17
18
19

If you observe the output closely you find that the loop has skipped 15 and 20.

General Problems while using Loops

Infinite Loop

The code goes into an infinite loop whenever we do not specify the termination condition. This problem causes a system crash.

Garbage Value as Output

The code outputs garbage values(big random numbers) when we do not initialize a variable.

Conventional Problems to master loops

Whenever you ask a programmer to test your basic coding skills, the first one that strikes his mind is pattern printing. With using loops we can print different patterns.

Illustration

Let us print a pattern of square with side 4 using and(&).

#include <stdio.h>
int main() {
for(int k = 1;k<=4;k++)
{
for(int l = 1;l<=4;l++)
{
    printf("&");
}
printf("\n");
}
return 0;
}Code language: C/AL (cal)

Output

&&&&
&&&&
&&&&
&&&&

You can also try patterns of right-angle triangles, Inverted right-angle triangles, Equilateral triangles, and so on. Practice those on your own to get maximum output.

Conclusion

Loops are pretty much used everywhere. Without loops, we cannot solve even basic problems in competitive programming. As every problem needs to be run for different test cases, we need to definitely use loops. From this article, you will get a brief idea of how to use loops, what are different kinds of loops and loop-control statements.

Happy Coding!

Sharing is caring

Did you like what Krishna Bethina wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: