Search This Blog


Showing posts with label looping statement. Show all posts
Showing posts with label looping statement. Show all posts

Monday, September 30, 2019

Looping statement OR Iterative statement



This statement is used to repeat a block of statements multiple times according to the specify condition.


Any loop consists of three parts :-

           I.            Initialization part
         II.            condition part
       III.            increment/ decrement part

⇒Types of Looping statements :-

  There are three loops in C and C++

                I.            for loop
              II.            while loop
            III.            do – while loop ( exit control loop )

➤For Loop :-

  •   In this loop we can specify initialization part , condition  and increment / decrement part at a time .
  •   It is a type of entry control loop because condition is checked first.
  •  At first initialization part is executed.
  •  After that condition is checked and then block of stmts is executed.
  •  And then after increment/ decrement is executed and again condition is checked , and the loop continued.

  Syntax :-

   for( initialization ; condition ; increment / decrement )
    {
      block of stmts;
      }
 
looping statement for loop
syntax of for loop 

  

example 1:-

    
   for ( i=1 ; i <= 100 ; i++ )
    {
      printf( “ %d \n “, i );
    }

  

Example 2:-

    
   i = 1;
    for(   ;  i <= 100 ;  )
   {
     printf( “ %d \n “ , i );
    i++;
    }

⏩Standard form of for loop

         
           I.            Infinity for loop
                for (  ;  ;  )
         {
           printf( “  Saurav “ ) ;
          }
           II.            for ( i=1 , j = 1 , k = 1 ; condition ; i++, j++ ,k++ )
{
  block of stmts ;
  }
         III.            for (  ;  condition  ;  )
{
  block of stmts ;
}

➤While Loop :-

  • In this loop all the three parts are specified separately.
  • It is a type of entry control loop because condition is checked first .
  • At first initialization part is executed.
  • After that condition is checked and then block of stmts is executed.
  • And then after increment/ decrement is executed and again condition is checked , and the loop continued.

Syntax :-

   initialization
  while( condition )
    {
     block of stmts ;
     increment/ decrement;
   }
looping statement whle loop
syntax of while loop

Example:-

   i=1;
  while ( I <= 100 )
  {
    printf( “ %d\n “, &i);
    i++;
  }

➤Do – While loop :-

  • In this loop all the three parts are specified separately.
  • It is a type of exit control loop because block of stmts is executed before the condition .
  • At first block of stmts  part is executed.
  • After that increment/ decrement part is executed .
  • Then after condition is checked , if it is true then the block of stmts is executed again and the loops continued.

Syntax :-

   initialization;
    do
  {
    block of stmts ;
    increment / decrement ;
   }
  while ( condition );
looping statement do while loop
syntax of do-while loop

  

Example :-

   i=1;
   do
   {
     printf( “ %d \n “, &I );
     i++;
    }
   while ( i <= 100 );


Let us know how different these loops are in programming.

P1. Program to print the series of natural no. upto 100 by for loop.

 #include<stdio.h>
#include<conio.h>
  void main()
 {
   clrscr();
   int i;
   printf( “ The series of natural no. upto 100 are \n “);
  for( i =1 ; i <= 100 ; i ++ )
    printf( “ %d \n “, i );
  getch();
 }
For loop programming of natural number
Program to print the 100 natural no.  By for loop

P2. Program to print the series of natural no. upto 100 by while loop.

  #include<stdio.h>
  #include<conio.h>
    void main()
   {
     clrscr();
     int i;
     printf( “The series of natural no. upto 100 are \n “);
      i=1;
      while( i <= 100 )
     {
      printf( “ %d\n “, i );
      i++;
     }
    getch ();
   }
While loop programming of natural number
Program to print the 100 natural no. By while loop

P3. Program to print the series of natural no. upto 100 by do - while loop.

  #include<stdio.h>
  #include<conio.h>
    void main()
   {
     clrscr();
     int i;
     printf( “The series of natural no. upto 100 are \n “);
      i=1;
       do
        {
         printf( “ %d\n “, i );
          i++;
         }
       while( i <= 100 );
    getch ();
   }
Do while programming of natural number
Program to print the 100 natural no. By do while loop


We practice more programs of looping statement further.