An Introductory Guide to Loops in C Programming Language

Share this Content

Hey, there. How are you?

As you can see, here we’ll talk about “Loops in C”. But before that, if you didn’t read all articles on C language from here.

Let me ask you a very simple question.

Print an Arithmetic Progression series {A.P series= 1,2,3,4,5,…}. But there are some conditions.

  • First term(a)=1
  • Common Difference(d)=2
  • n=10

This means the program will run until unless ‘n’ becomes 100.

So, how do you do this?

In these types of cases, we’ve to use a new term, which is called Loop.

Definition:

Or you may ask,

What does a loop do in C programming?

Now from the name itself, we can clearly understand that some repetition is going to happen here.

While writing C codes sometimes it happens that we want to run an instruction or a bunch of instructions until a particular condition is satisfied or we got our preferable answer.

In those cases we use loops.

Loops can be finite and infinite as well.

Now I hope the definition of “Loop” is clear to you.

Now let’s answer the question.

#include <stdio.h>
int main(){  
  int a=1,d=2,n,s;   
  printf("Required series is-n");  
  for(n=1; n<=10; n++)   
  {     
    s=a+(n-1)*d; 
    printf("%d ",s);  
  }   
  return 0;
}

Now let’s run this program and see what we got.

Required series is- 1 3 5 7 9 11 13 15 17 19

See, how easily we got the A.P series using a loop.

This is a finite loop, as we can see clearly.

Types of Loops:

In C, we’ve 4 types of loops.

  1. For loop
  2. While loop
  3. do…while loop
  4. nested loop:
    • Nested while Loop
    • Nested for loop
    • Nested do…while loop

As we’ve already introduced with for loop, so let’s start with for loop.

For Loop

Definition of for loop

In the example, I’ve that it was a finite loop. It means, in ‘for loop’ we can control the repetition.

So,‘ for loop’ is nothing but a simple loop structure that allows us to control the repetition.

Syntax of for loop

You can see the syntax of ‘for loop’ from the given example as well.

for ( init count; condition; increment ) { instructions(/statements); }

Now let’s see how “for loop” works.

  1. In the init count part we give the initial value of the loop counter. In the loop, this step is executed first and only once.
  2. The very next step which is executed is the condition part. If the condition gets satisfied, only then the body of the loop is executed.
  3. After the body of the loop is executed successfully, the next step is the increment statement. Every time the body of the loop has been executed, the ‘increment’ statement increases the value of the loop each time.
  4. This process will run continuously until the condition becomes false.

So, this is how the ‘for loop’ works.

Now, the second type of loop is-

While Loop

Print Hello World 10 times

#include <stdio.h> int main() { // initialization expression int i = 1; // test expression while (i <= 10) { printf("Hello Worldn"); printf("It is a nice day, isn't it?n"); // update expression i++; } return 0; }

The output will be-

Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

Now let’s see what is happening here.

  1. In the initialization expression, we basically initialise the value of i as 1.
  2. Then we gave the condition to the compiler that as long as i<=10 the loop will run. After that, it will go outside the loop to return 0.

Definition of while loop

So, from the example, we can say that the While loop is something that repeatedly executes a statement or a bunch of statements(like the example) as long as the condition is true.

From the definition and the given example, we can clearly understand that the While loop can be infinite. If you want to see, then just put “i” in the place of “i<=10” in the code. Then see what happens and tell me in the comment box.

Syntax of while loop

From the example, the syntax of the While loop is

Subscribe to Tech Break

while(condition) { statement; }

Now our next type of loop is-

Do while loop

This loop is quite similar to the while loop.

There is just one difference between these loops and that is –

In do…while loop the condition appears at the end/bottom, wherein others we write the condition at the top of the loop.

As it is quite similar to the while loop, so its definition is also quite the same. Just keep two things in mind.

  • In do…while loop the condition appears at the end/bottom.
  • ‘do..while loop’ does the execution faster than ‘while loop’.

For example, let’s take the same example of “while loop”.

#include <stdio.h> int main() { // initialization expression int i = 1; // do loop expression do{ printf("Hello Worldn"); printf("It is a nice day, isn't it?n"); // update expression i++; } while (i <= 10); return 0; }

Now see here we give the condition at the bottom of the loop(also outside the loop).

If we run this code, we’ll get the same answer as the while loop.

Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

Syntax of do…while loop

So, its syntax is very simple.

do{ instructions; } while(condition);

Now we’ll talk about-

Nested Loops

Definition

Have you ever seen the nest of any bird?

If yes, then the nested loop will become very easy for you to understand.

In a nest, there are lots of branches. Just like that in a nested loop, we can use branch loops, which means loops inside another loop.

Types

Nested for loop

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

Nested while loop

while(condition) { while(condition) { instruction; } instruction; }

Nested do…while loop

do{ instruction; do{ instruction; } while(condition); } while(condition);

So, these are the normal or general forms of nested loops. But it doesn’t mean that we can’t use a different loop inside another loop.

Remember, there is no rule that a nested loop will be only made by its own type of loop. In fact, any kind of loop can be nested inside another loop, for example, we can use for loop inside a while loop or vice versa.

do{ while(condition) { for ( initialization; condition; increment ) { instruction; //for loop } instruction; // while loop } instruction; // do...while loop }while(condition);

Now as we are upgrading ourselves in coding, so this example is going to be an intermediate level. But don’t worry, just understand the syntaxes and overall code.

For our next example, you need to know about Arrays. So, if you don’t know about it then I’ll suggest learning Array first then jump into the example.

Make two matrices and add them, Then print the result.

#include <stdio.h> 
int main() {
  int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  //   printf("Enter the number of rows (between 1 and 100): ");
  scanf("%d", &r);
  printf("Enter the number of columns (between 1 and 100): ");
  scanf("%d", &c);    printf("nEnter elements of 1st matrix:n");
  //Nested for loop 
  for (i = 0; i < r; ++i) 
    for (j = 0; j < c; ++j)  
    {       
      printf("Enter element a%d%d: ", i + 1, j + 1);  
      scanf("%d", &a[i][j]);
    }
  printf("Enter elements of 2nd matrix:n");  
  for (i = 0; i < r; ++i)     
    for (j = 0; j < c; ++j) 
    {       
      printf("Enter element b%d%d: ", i + 1, j + 1);
      scanf("%d", &b[i][j]);   
    }  
  // adding two matrices  
  for (i = 0; i < r; ++i)    
    for (j = 0; j < c; ++j)  
    {      
      sum[i][j] = a[i][j] + b[i][j]; 
    }   
  // printing the result
  printf("nSum of two matrices: n");
  for (i = 0; i < r; ++i)    
    for (j = 0; j < c; ++j)  
    {     
      printf("%d   ", sum[i][j]);   
      if (j == c - 1)    
      {        
        printf("nn");  
      }
    }
  return 0;
}  

If we run this code, let’s see what happens.

Enter the number of rows (between 1 and 100): 2 Enter the number of columns (between 1 and 100): 2 Enter elements of 1st matrix: Enter element a11: 2 Enter element a12: 1 Enter element a21: 3 Enter element a22: 2 Enter elements of 2nd matrix: Enter element b11: 1 Enter element b12: 2 Enter element b21: 2 Enter element b22: 1 Sum of two matrices: 3 3 5 3

In this example, we’ve seen the use of nested for loop and add two matrices successfully.

So, guys that are for Loops in C language. I hope, I could make this chapter easy and understandable to you.

But before leaving, I want to give you a question.

Multiply two matrices and print the result.

Share this Content
Snehasish Konger
Snehasish Konger

Snehasish Konger is the founder of Scientyfic World. Besides that, he is doing blogging for the past 4 years and has written 400+ blogs on several platforms. He is also a front-end developer and a sketch artist.

Articles: 192

Newsletter Updates

Join our email-newsletter to get more insights

Leave a Reply

Your email address will not be published. Required fields are marked *