Loop in C | for, while and do while loop with examples




Loop in C Language



In this page you will learn about What is Loop in C programming? How many types of Loop in C programming? What is Data Flow Diagram of Loop in C programming? What are examples of Loop in C programming? After reading this page you completely able to define about Loop in C programming languages.

    loop in c programming
    Loop in C


    Loop in C language is the one of the most useful looping making statement in real time programming. It gives ability to perform a set of instruction repeatedly. This involves repeating some portion of the program either a specified number of times or until the certain condition is being satisfied. This repetitive operation is done through a loop control structure. It reduces lengthy code in its simple form and the work load of programmer. 


    Types of Loop:

    Loop in C program/Types of Loop in C
    Types of Loop in C

    1. Entry Control Loop


    Entry Control Loop means that the condition is checked first and then execute the body of loop. while Loop and for Loop comes under it.

    while Loop


    while Loop is type of Entry Control Loop. It simple type of loop. It is a loop that repeat a set of statement based upon a condition. As far as loop returns Boolean value of True, the statements inside the body of loop will keep repeating. It also reduces the code and load of programmer. It makes program easy and simple to understand. We use this type of loop when we don't know the exact number of times a code need to be executed.

    while(Condition expr)
    {
          // statements that we want to execute
         //increment
    }

    Data Flow Diagram of while Loop

    Loop/Loop in C Language/Data flow diagram of while loop
    DFD of while Loop
                          




    Example: Write a C program to find table of entered number by user using while loop. 


    int main()
    {
        int n,i=1;
        printf("Enter the valid no. to find its table:\n");
        scanf("%d",&n);
        printf("\nTable of %d:\n", n);
        while(i<=10)
        {
            printf("\n %d * %d = %d", n, i, i*n);
            i++;
        }
        return 0;
    }
    
    Enter the valid no. to find its table:
    8
    Table of 8:
     8 *  1 = 8
     8 *  2 = 16
     8 *  3 = 24
     8 *  4 = 32
     8 *  5 = 40
     8 *  6 = 48
     8 *  7 = 56
     8 *  8 = 64
     8 *  9 = 72
     8 * 10 = 80

    for Loop


    for Loop is a type of Entry Control Loop. It is a repetition control structure which allow us to write a loop which is executed a fixed number of times. It enables us to perform number of steps together in one block of statements. It makes complex and lengthy programs code into its simplest form. It reduce the length of code and time and also reduces the load of programmer. We use this type of loop when we know the exact number of times a code need to be executed.

    for(initialization expr; Condition expr; Update expr )
    {
          // statements that we want to execute
    }
    

    Data Flow Diagram of for Loop

    Loop/Loop in C Language/Data flow diagram of for Loop
    DFD of for Loop
                        




    ExampleWrite a C program to print all alphabets from a to z using for loop. 


    int main()
    {
        int i;
        printf("List of alphabet from a to z:");
        for(i=97;i<=122;i++)
        {
            printf(" %c ",i);
        }
        return 0;
    }
    Lists of alphabet from a to z:
     a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

    2. Exit Control Loop


    Exit Control Loop means that the body of the loop executed once before checking the condition. Do while Loop comes under it.

    do while Loop


    do while Loop is a type of Exit Control Loop. It is similar to while loop with one important difference is that it executes its codes once before checking the condition expression. Since do while loop is executed at least once. 
    do
    {
          // statements that we want to execute
         // increment
    }
    while(Condition expr);

    Data flow Diagram of do while Loop

    Loop/Loop in C Language/Data flow diagram of do while loop
    DFD of do while Loop



    Example: Write a C program to find factorial of entered number by user using do while loop. 


    int main()
    {
        int fact=1, num, temp;
        printf("Enter the nubmer to find its factorial:\n");
        scanf("%d",&num);
        temp=num;
        do
        {
            fact=fact*num;
            num--;
        }
        while(num>0);
        num=temp;
        printf("Factorial of entered no. %d is %d",num, fact);
    }
    
    Enter the nubmer to find its factorial:
    7
    Factorial of entered no. 7 is 5040
    

    Conclusion:

    In this article, we have thoroughly read about Loop in C programming with brief description of each types of Loop in C programming. Types of Loop in C programming are:


    Exit Control Loop
    • while loop
    • for loop
    Entry Control Loop
    • do while loop

    If you are interested then also read:

    Top 10 C program with output for practice

    Flowchart and its different symbol used in making Data Flow Diagram in C

    If you find this page helpful then please share!


    Tags: Loop in C, Loop in C programming, Types of Loop in C, Data flow diagram of Loop in C, Examples of Loop in C, Example programs of Loop in C


    No comments:

    Powered by Blogger.