Monday, October 6

Iteration Constructs

The For Loop


Source Code :



// Program Description:
// A Program that uses for iteration construct to print a series

#include<iostream.h>
#include<conio.h>

void main()
{
// cleaning the screen
clrscr();


// declaration of variables
int ctr;


// for loop starts here
for(ctr=0;ctr<=10;ctr++)
{
cout<<endl<<"\t"<<ctr;
}// for loop ends here


// waiting for the user to analyse the output
cout<<endl<<endl<<"\tPress any key to quit...";
getch();

}// main ends here


Program Flow :


  • A counter variable is created in the memory. This variable's value acts as the termination statement for the loop.
  • The Looping Statement is encountered by the compiler. In this statement the compiler checks for the counter variable's initial value to enter the loop.
  • If the Loop Condition is fulfilled by the counter variable the block of code which is placed inside the curly braces gets executed.
  • The Loop then comes back to the Conditional Statement and checks whether the counter variable's value still fulfills a certain criteria.
  • If the value of counter still fulfills the condition the Loop is executed again.
  • This process continues until the Counter's value fulfills the condition.
  • The Compiler exits the Loop and waits for the user to press a key to exit.

Output Screenshots :




No comments:

Post a Comment