Monday, October 6

Iteration Constructs

The While Loop


Source Code :



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

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

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


// declaration of variables
int ctr = 0;


// while loop starts here
while(ctr<=5)
{
cout<<endl<<"\t"<<ctr;
ctr++;

}// while 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 :



This Iteration Construct is a bit different from the For Iteration Construct because it does not have the Initialising Condition in the Loop statement and the Increment/Decrement Statement is also absent. Instead the Counter Variable is Initialised outside (before) the Loop and the Increment/Decrement statement is present inside the Loop block.

  • The Loop Statement checks the Counter's Value with the specified condition.
  • If the Condition evaluates to true the compiler enters the Loop's body and executes the whole block and then comes back to the conditional statement and the cycle goes on.
  • Once the False condition is met the Compiler exit the Loop and moves on.
  • The compiler then waits for the user to press any key to exit.


Output Screenshots :


No comments:

Post a Comment