Monday, 26 March 2018

The Loop control Structures

Many times it is necessary to execute several statement repetitively for certain number of times. In such case, we can use looping statement of Cpp programming language. The statement are executed until a condition given is satisfied. Depending upon the position of the condition in the loop, the loop control structures are classified as the entry-controlled loop and exit-controlled loop. They are also called as pre-test and post-test loops respectively.

The 'while' loop
   The 'while' is an entry-controlled loop statement having following general form:
   while(condition)
    {
      //loop statement or body of loop
     }
        Here, the condition is evaluated first,if it is true then loop statements or body of the loop is executed.After this the program control will transfer to condition again to check whether it is true or false.If true,again loop body is executed.This process will be continued until the condition becomes false.When is becomes false, the statements after the loop are executed.This is also called as pre-test loop. 


The do-while loop
  This 'do-while' is exit-controlled loop statement in which the condition of the loop statement is written at the end of the loop.It takes the following general form:
    do
     {
      //loop statement or loop body
     }while(condition)
Here, when the program control is reached at do statement the loop body or statements inside the loop are executed first.Then at the end the condition is the 'while' is checked.If it is true, then program control again transfers to execute the loop statements.This process continues until the condition becomes false.In short, we can say that the loop statements are executed at least once without checking condition for its trueness.


     
The 'For' loop
  The 'for' loop is an entry controlled loop structure which provides more concise loop structure having following general form:
  for(initialization ; condition ; increment/decrement)
  {
   //loop statement or loop body
   }
 The execution of 'for' loop statement takes as follows:
  1. Initialization of loop control variables is done first using assignment operators such as: i= 1 or count = 0 etc. Remember this part is executed only once.
  2. The condition given afterwards is checked then. If this condition is true, the statements inside the loop are executed.Else program control will get transferred nest statement after the loop.The condition can be combination of relational as well as logical operators such as :
count < 10
  3. When the body of the loop is executed, program control will get transferred back to 'for' statement for executing the third statement that is, 'increment/decrement'. In this part the loop control variable's value is incremented or decremented.Then program controls the condition again to check whether it is true or not. If true the loop body executed again.And the process is continued again until condition evaluates to false.

0 Comments:

Post a Comment

Popular Posts

Categories

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