C++ LOOPS

Loop: Loop are used to execute a statement or a set of statement either a specific number of time or until a particular condition is being executed.

The repetitive operation is done through a loop control instruction.

Types of loops :-

Depending upon the position of a control statement in a program a loop is classified into two types :-

  1. Entry controlled loop.
  2. Exit controlled loop.

Entry controlled loop : In this loop a condition is checked in the beginning of the loop body.

While loop & for loop are entry controlled loop.

Exit controlled loop : In this loop , a condition is checked at the end of the loop body.

Do- while loop is an exit controlled loop.

C provide three types of loop:-

  1. The while loop
  2. The for loop
  3. The do-while loop.

While loop:  In this loop the condition is given in the beginning of the loop and all the statements are executed till the given condition satisfies.

And when the condition becomes false, the control will move out of the loop.

After exiting the loop , the control may move to the next statement if present.

The body of a loop can contain more than one statement.

In a while loop, if the condition is not true then the body of a loop will not be executed not even once. It is different in the do-while loop.

Syntax:

While (condition) 
       {
        Statement;
       }

Example:

// Program to print series of numbers 1 to 10 using while loop.

# include <iostream.h>
# include <conio.h>
int main()
{
    int num=1; // initializing the variable
    while(num<=10) // while loop wi/n condition
        {
         cout<<"\n">>,num;
        num++;              //increment operation
        }
return 0;
}

for Loop: This loop has similar functionality as while loop but with different syntax.

For loops are preferred when the number of timer loop statements are to be executed is known before -hand.————-

The loop variable initialization, condition to be tested ,and increment, decrement of the loop variable is done in one line in for loop there providing a shorter , easy to debug structure of looping.

Syntax:

 for(initial value;Condition;increment/decrement)
        {
         Statement;
        }

Example:

// Program to print series of numbers 1 to 10 using for loop.

  #include<iostream.h>
  #include<conio .h>
    int main ()
    {
     int i;
     for (i=1; n c=10; n++)  // for loop to print 1-10 number
         { 
           cout<<"\n">> ,10;      // to print the number 
         }
    return 0;
    }

do-while loop: The loop is similar to while loop with the only difference that it checks the condition after executing the statement , i.e. it will execute the loop body one time for sure because it checks the condition after executing the statements.

Syntax:

do{
   Statements
  {
While(condition)
  }

Example:

// Program to print 1 to 10 using do-while loop.

#include<iostream.h>
#include<conio.h>
int main()
{
 int  i=1;      // initializing the unable 
    do            // do-while loop
     {
         cout<<"n">>,i;
      i++;        // increment operation.
     }
     while (i<=10);
     return 0;
}