Nested Loops in C

Beginner

Introduction

A loop is a statement that allows code to be executed repeatedly based on a particular condition. In C, the for loop is frequently used, especially for performing iterations with a particular number of times. The concept of a nested loop is where one loop is positioned inside the loop block of another loop. This programming is used to obtain the desired output pattern. In this lab, we will learn how to perform nested loops in C programming.

Create a new C file

Open your terminal and create a new C file named main.c, by running the following command:

touch ~/project/main.c

You can choose to use any other directory instead of ~/project/, or create the file manually using a text editor.

Write the initial C program

To start with, let us write a simple C program that uses nested loops to print a pattern of "*" separated with white space. Here's the code:

#include<stdio.h>

int main()
{
      printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
      printf("\n\nNested loops are usually used to print a pattern in c. \n\n");
      printf("\n\nThey are also used to print out the matrix using a 2 dimensional array. \n\n");

      int i,j,k;
      printf("\n\nOutput of the nested loop is :\n\n");
      for(i = 0; i < 5; i++)
      {
         printf("\t\t\t\t");
         for(j = 0; j < 5; j++)
         printf("* ");

         printf("\n");
      }
      printf("\n\n\t\t\tCoding is Fun !\n\n\n");
      return 0;
}

Understanding the code

Before proceeding, let's understand the structure of the program.

First, we have included the header file "stdio.h" which contains standard input and output functions like printf() and scanf().

Next, inside the main function, we have printed three messages, "LabEx - Best place to learn", "Nested loops are usually used to print a pattern in c.", and "They are also used to print out the matrix using a 2 dimensional array." on the console using printf() function.

After that, we have initiated three variables i, j, and k to use in the loop. Inside the nested for loop, we have used two for loops. The outer for loop controls the number of rows and the inner for loop controls the number of columns.

Now, let's discuss the working of nested loops. The outer loop initializes the i variable to 0. It checks if i < 5. If it is true, the control passes to the inner loop which initializes the j variable to 0. The inner loop checks if j < 5. If it is true, it prints an asterisk with a space, then increments j.

Once the inner loop is executed completely, the control is returned to the outer loop and the value of the i variable is incremented. This continues until the i variable becomes 5, and both loops terminate execution.

Modify the code

Now, it's time to modify the existing code to create your own output pattern. Try creating patterns of your choice by modifying the variables "i" and "j." For instance, you can print a pattern of square '*' separated with white space by modifying the program as shown below:

#include<stdio.h>

int main()
{
      printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
      printf("\n\nNested loops are usually used to print a pattern in c. \n\n");
      printf("\n\nThey are also used to print out the matrix using a 2 dimensional array. \n\n");

      int i,j,k;
      printf("\n\nOutput of the nested loop is :\n\n");

      for(i=0;i<5;i++)  // outer loop
      {
         printf("* ");
         for(j=0;j<4;j++)  // inner loop
         printf("* ");
         printf("\n");
      }

      printf("\n\n\t\t\tCoding is Fun !\n\n\n");
      return 0;
}

Compile and run the code

After making the desired changes to the program, save the file, and compile it using the following command in your terminal:

gcc main.c -o main

Once you run the above command, it will compile your code, and if there are no errors, it will generate an executable file named "main."

Now, execute the code by running:

./main

Summary

In this lab, we learned about nested loops in C. We discussed the concept of a nested loop, its importance, and how it can be used to obtain the desired output pattern. We went through step-by-step instructions to create a C program using nested loops. In conclusion, nested loops play an important role in C programming as they are used to handle tables, multi-dimensional arrays, and uppercase characters.

Other Tutorials you may like