Thursday, 27 July 2017

Nested Loop And Types Of Nested Loop In C Programming



Nested Loop And Types Of Nested Loop In C Programming.

Nested Loop In C:

A loop inside another loop is called a nested loop. The intensity of nested loop relies upon at the complexity of a problem. We can have any number of  of nested loops as required. Don't forget a nested loop wherein the outer loop runs n times and includes some other loop inside it. The inner loop runs m times. Then, the whole number of times the inner loop runs throughout this program execution is n*m.

We also define the "Nested Loop" as:

A nested loop is a loop inside a loop, an inner loop in the body of an outer one. How this works is that the first pass of the outer loop triggers the internal loop, which executes to final touch. Then the second pass of the outer loop triggers the internal loop once more. This repeats till the outer loop finishes. Of direction, a ruin inside either the inner or outer loop might interrupt this process.

Types of nested loop:

There are three types of Nested Loop are:

1 - Nested While Loop
2 - Nested Do-While Loop
3 - Nested For Loop

Nested While Loop:

A while loop inside another while loop known as nested while loop.

Syntax of Nested while loop.


while (condition1)
{
    statement(s);
    while (condition2)
    {
        statement(s);
        ... ... ...
    }
    ... ... ...
}

Flowchart of Nested While loop:



Example of Nested While loop:

C program to print the number pattern.

Code:

#include <stdio.h>
int main()
{
    int i=1,j;
    while (i <= 5)
    {
        j=1;
        while (j <= i )
        {
            printf("%d ",j);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

Output:

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

In this program, nested whilst loop is used to print the pattern. The outermost loop runs five times and for every loop, the innermost loop runs i times that is 1 at the start, which means most effective "1" is outlined, then on the next loop it is 2 numbers printing "1 2" and so forth until five iterations of the loop executes, printing "1 2 3 four 5". This manner, the given number pattern is printed.

Nested Do-While Loop:

A do-while loop inside another do-while loop is known as nested do-while loop.


Syntax of Nested Do-While Loop.

do
{
    statement(s);
    do
    {
        statement(s);
        ... ... ...
    }while (condition2);
    ... ... ...
}while (condition1);

Flowchart of Nested While loop:



Example of Nested Do-While loop:

C program to print the given star pattern.

Code:


#include <stdio.h>
int main()
{
    int i=1,j;
    do
    {
        j=1;
        do
        {
            printf("*");
            j++;
        }while(j <= i);
        i++;
        printf("\n");
    }while(i <= 5);
    return 0;
}

Output:

*
**
***
****
*****


On this program, nested do-while loop is used to print the star pattern. The outermost loop runs five times and for every loop, the innermost loop runs i times that's 1 at the beginning, which means best one "*" is outlined, then on the next loop it's 2 printing two stars and so on until five iterations of the loop executes, printing 5 stars. This way, the given celebrity pattern is printed.

Nested For Loop:

A for loop inside another for loop is called nested for loop.

Syntax of Nested For Loop.

for (initialization; condition; increment/decrement)
{
    statement(s);
    for (initialization; condition; increment/decrement)
    {
        statement(s);
        ... ... ...
    }
    ... ... ...
}

Flowchart of Nested For loop:

Example of Nested for loop:

Write a Pattern Programs Using Nested For loop

Code:
#include<stdio.h>

#include<conio.h>

void main()
{
int n,r,c;
 for(r=3;r<=6;r++)
 {
  for(c=1;c<=r;c++)
 {
  printf("*");
 }
  printf("\n");
 }
 for(n=5;n>=3;n--)
 {
 for(c=1;c<=n;c++)
 {
 printf("*");
 }
 printf("\n");
 }
 getch();
 clrscr();
}
Output:

Using these examples you are in a better position to create your desired pattern for yourself. Creating a program involves how to use nested loops properly, some pattern may involve alphabets or other special characters. Key aspect is knowing how the characters in pattern changes.

Try it yourself.
If you have any problem you can tell me on comment box i will help you.




    Viral Programs

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

    5 comments:

    1. Hi Kamran

      Nice article which helps reader like me thanks for sharing and keep updating you workingthank you.

      ReplyDelete
    2. Thanks Manish,
      I'll try my best to improve my work.

      ReplyDelete
    3. It’s Amazing post for nested loop users. Thanks for sharing with us. I love the way you write. pls visit me blog www.subtechguide.com

      ReplyDelete
    4. thanks to support me i will visit your blog as soon as possible.

      ReplyDelete
    5. Hi ! very good article.
      Best study and learnig.
      thanks for sharing good keep it up....

      ReplyDelete