Sunday, 14 August 2016

Looping in c++

A loop is defined as a block of statement ,which are execute for a certain no. of times in a repititive manner.the loops are of two type where,

  1. Definite loop =In which the block of statement are executive according to the counter control repitition .
  2. Indefinite loop= In which the program is executive in such a way where the total number of iteration cannot be calculated 

A sequence of statement is executed until a specified condition is true.This sequence of statement to be executed is kept inside the curly braces {} known as loop body.After every execution of loop body,condition is checked ,and if it is found to be true the loop body is executed again,  when condition check comes out to be false,the loop body will not be executed .

                                      THERE ARE THREE TYPES OF LOOPS IN C++


  1. WHILE LOOP
  2. DO-WHILE LOOP
  3. FOR LOOP
  4. NESTED FOR LOOPS
WHILE LOOP

The while loop is frequently used in programs for the repeated execution of statement in a loop .Until a certain  condition is satisfied the loops statement are executed ,while loop is also known as entry control loop,because it only execute the statement if the test condition is true ,so if the condition is false the compiler will not enter in the body of while loop and does not execute its statement.

SYNTAX:

{
Initialization of variable
while(condition)
{
body of while loop
increment/decrement counter
}
}

 EXAMPLE:

void main()
{
int x=1;
while(x<10)
{
cout<<"value is ":<<x;
x++;
}
}

                                                     DO-WHILE LOOP

The do -while loop is also known as exit control loop.,which me ans that the body of the loop is executing atleast once even if the test condition is false because the test condition is given at the ending of the loop.The test condition is tested for the next time to execute the loop .The basic
difference between while and do while is that while loop is executed only if the test condition is true whereas do-while executes atleast once as we check the test condition at the end which desire the re-entry (only if it is true)inside the loop.


SYNTAX:

void main()
{
initialization variable;
do
{
body ;
increment/decrement;
}
while(test condition)
}


EXAMPLE:

void main()
{
int x=1;
do
{
cout<<"value is :"<<x;
x++;
while(x<10)
}
getch();
}


                                                             FOR LOOP

FOR loop allow to execute a set of instructions until a certain conditions is  satisfied . Condition may be predefined or open-ended.The general syntax of the FOR loop will be as given below.

explanation:
the FOR statement contains three expressions which are separated by semicolons.Following actions are to be performed in the three expressions.

SYNTAX:
for(initialize counter;test condition ; re-evaluation parameter)
{
     statement 1;
     statement 2;
}




  1. The initialize counter sets to an initial value. This statement  is executed only once.
  2. The test condition is a relational expression that determines the number of iterations desired or determine when to exit from the loop . The FOR loop continuous to execute as long as conditional test is satisfied .When the condition becomes false the control of the loop program exists from the body of the  FOR  loop and executes next statement after the body of the loop.
  3. The re-evaluation parameter decided how to make changes in the loop (increment or decrement operations are to be used quite often).
  4. The body of the loop may contain either a single statement or multiple statement s.In case, there is only one statement after the FOR loop, braces may not be necessary .In such a case ,only one statement is executed till the condition is satisfied .It is good practice to use braces even for single statement following the for loops.
EXAMPLE :

   void main ()
{
  int i;

  clrscr ( );
 
  for(int i=1;i<=5;i++)

  cout<<"number: ",i,i*i);
}

                                                       NESTED FOR LOOPS

We can also use loop within loops ,In nested  FOR loops one or more FOR statements are included in the body of the loop . In other words c++ allows multiple FOR loops in nested forms.The numbers of iterations in this type of structure will be equal to the number of iteration in the outer loop multiplied by the number of iteration in the inner loop . Given below examples are based on the nested FOR loops.

EXAMPLE:

Write a program to perform subtraction of two loop variables.use nested for loop.

void main()
{
      ina a,b,sub;
     clrscr();
 
       for(a=3;a>=1;a--)
  {
        for(b=1;b<=2;b++)
     {
        sub=a-b;
       cout<<"a=%d a-b= %d"<<a,b,sub;
     }
  }

}


No comments:

Post a Comment