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);
      }
    }
  

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (725) Python Coding Challenge (169) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses