Sunday, 1 April 2018

Loop Control Structures

These are three methods by way of which we can repeat a part of a program in C programming.
1. Using a for Statement
2. Using a while Statement
3. Using a do-while Statement

The While Loop (pre-test Loop)
   The general form of while is as shown below :
     initialize loop counter ;
     while (test loop counter using a condition)
     { 
         do this;
         and this;
         increment loop counter;
     }
The parentheses after the while contains so long as this condition remains true all statements within the body of the while loop keep getting executed repeatedly for e.g.
  /* calculate simple interest for 3 sets of p, n and r */
     main( )
     {
      int p, n, count;
      float r, si;
      count = 1;
      while(count <= 3)
       {
        printf("Enter values of p, n and r");
        scanf("%d%d%f", &p,&n,&r);
        si = p*n*r/100;
        printf("simple interest = Rs. %f" ,si);
        count = count +1;
       }
     }

The while construct consists of a block of code and condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.
 For example, in the C programming language (as well as Java  and C++, which use the same syntax in this case), the code fragment
  x = 0;
  while <x < 5)
     {
         printf("x = %d\n", x);
         x++
     }
first checks whether x is less than 5, which it is, so then the {loop body} is entered, where the printf function is run and x is incremented by 1. After completing all the statements in the loop body, the condition, (x < 5), is checks again, and the loop is executed again, the process repeating until the variable x has the value 5.
  Note that it is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop.


The do-while Loop (Post-test loop)
  The do-while loop takes the following form
   do
   {
     this;
     and this;
     and this;
     and this;
   } while ( this condition is true);

There is a minor difference between the working of while and do-while loops. The difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this the do-while tests the condition after having executed the statements within the loop. 
        
                           The do while construct consists of a block of code and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed
   
         It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure(such as a break statement) that allows termination of the loop.
            Some languages may use a different naming convention for this type of loop. For example, the Pascal language has a "repeat until" loop, which continues to run until the control expression is true (and then terminates) - whereas a "do-while" loop runs while the control expression is true (and terminates once the expression becomes false).

The for loop
  The general form of for statement is as under :
    for(initialize counter; test counter, increment counter)
    {
      do this;
      and this;
      and this;
    }

Now let us write the simple interest problem using the for loop .

 /* Calculate simple interest for 3 sets of p, n and r */
  main( )
  {
    int p,n,count;
    float r, si;
    for(count=1; count <=3; count = count + 1)
     { 
       printf("Enter values of p, n and r");
       scanf("%d%d%f",&p, &n, &r);
       si = p*n*r / 100;
       printf("Simple interest = Rs. %f" , si);
      }
    }
  

1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)