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

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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