Introduction to while Loops in C

Discussion in 'C' started by lionaneesh, Jul 2, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    The while loop is a statement used in some programming languages (including C) to execute code repeatedly, this allows a programmer to execute a set of instruction multiple times until a condition is met.

    It is one of the simplest looping structure in C and basically follows the following format:-

    Code:
     while (condition)
    {
    Code to execute
    }
    
    Note: The loop body can contain any number of statements and the braces (‘{’ and ‘}’) are only needed if the loop body contains more than one statement.


    The Working


    1. First the program check for the given test condition and if it evaluates to true then the ‘body’ Of the loop is executed.
    2. After execution the program once again checks if the test condition still evaluates to true and if it is true then the loop body is executed.
    3. This process of repeatedly execution continues until the test condition is evaluated to false.

    Constructing While loops



    Now that we know something about while loops lets use this knowledge to convert some English statements into pseudo code statements.
    1. Until it stops raining we’ll play football.

      Code:
      while(raining == true)
          {
              playFootball();
          } 
      
    2. While Arum is ill He will take rest.
      Code:
      while(arum == ill)
          {
              takeRest();
          }
      

    Using While Loops



    I think now we have a basic understanding on how to construct while loops, so lets use this knowledge to create a basic counting program which would simply print out ‘n’ natural numbers.

    Numbers.c
    Code:
    #include<stdio.h>
    
    int main()
    {
        int n = 100;
        int counter = 1;
        while(counter <= n)
        {
            printf("%d\n",counter);
            counter = counter + 1;
        }
        return(0);
    }
    
    Explanation:-

    1. First we declare and initialize 2 variables ‘n’ and ‘counter’ , ‘n’ is the number of natural numbers we want to display and ‘counter’ is simply used to keep track of number of times the loop is executed.
    2. Then we add a while statement with a condition (counter <= n) , Which means print out the numbers until counter is less than or equal to n.
    3. In the loop body we print out the counter and we increment the counter every time the loop is executed.

    Output:-
    Code:
    1
    2
    3
    4
    5
    6
    -----------Sniped----------------
    
    That's all for this tutorial , Stay tuned for more
     
  2. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    good explanation sir

    i think we can rely on you
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks for appreciating! Do add thanks if you liked the article!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice