Recent

recentposts

Iteration Statement (Loops)

 Iteration Statement

Now suppose that user wants to execute certain statement more than one time then writing the same code again and again difficult for the programmer and the code also become more complex. For this reason , Iteration statements were introduced to reduce the complexity of code and also make the program easy .

The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled .The iteration statements are also called loops or looping statements.

There are three types of iteration or loops – for loop, while loop and do-while loop.

 

The for loop-

Syntax:

for(initialization-expression ; test-condition ; update-expression)

  {    statement to be executed  ;    }


loop


 

Program to print the odd numbers upto 100 using for loop.

#include<iostream.h>

#include<conio.h>


void main() {


clrscr() ;

int i = 1 ;


for( i ; i<100 ; i=i+2 )

{

cout<<i ;

}


getch() ;

}

 

Program to print the even numbers using for loop and ranges are taken by user.

#include<iostream.h>

#include<conio.h>


void main() {


clrscr() ;

int starting, ending ;


cout<<”\n Enter the starting range of Even number series. ” ;

cin>>starting ;

cout<<”\n Enter the end point of Even number series. ” ;

cin>>ending ;


for ( int i = starting ; i < ending ; i=i+1 )

  {


      if ( i%2==0 )

      {  cout<<”Even number = ”<<i ;   }


}


getch() ;

}

 

Program to identify Prime Number between 1 to 50.

#include<iostream.h>

#include<conio.h>


void main() {


clrscr() ;

int counter, divisor ;


for (int number=1 ; number < 50 ; number++ )

   {    counter = 0 ;

    

    for (divisor = number ; divisor>0 ; divisor-- )

{

     if ( number % divisor==0 )

     {  counter++ ;  }

     else

     {   continue ;   }

  }


  if ( counter == 2 )

  {  cout<<”\n Prime Number is = ”<< number ;  }

   

    }


getch() ;

}



__________________________________________________________________________

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Please give your valuable suggestions