IMPLEMENTATION OF CODE OPTIMIZATION TECHNIQUE

AIM:
To write a program for implementation of Code Optimization Technique in for and do-while loop using C++.

ALGORITHM:
Step 1: Generate the program for factorial program using for and do-while loop to specify optimization technique.
Step 2: In for loop variable initialization is activated first and the condition is checked next. If the condition is true the corresponding statements are executed and specified increment / decrement operation is performed.
Step 3:The for loop operation is activated till the condition failure.
Step 4: In do-while loop the variable is initialized and the statements are executed then the condition checking and increment / decrement operation is performed.
Step 5: When comparing both for and do-while loop for optimization do-while is best because first the statement execution is done then only the condition is checked.So, during the statement execution itself we can fin the inconvenience of the result and no need to wait for the specified condition result.
Step 6:Finally when considering Code Optimization in loop do-while is best with respect to performance.

PROGRAM:
Before:Using for:
#include<iostream.h>
#include <conio.h>
int main()
{
int i, n;
int fact=1;
cout<<"\nEnter a number: ";
cin>>n;
for(i=n ; i>=1 ; i--)
fact = fact*i;
cout<<"The factoral value is: "<<fact;
getch();
return 0;
}

OUTPUT:
Enter the number: 5
The factorial value is: 120

After:Using do-while:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,f;
f=1;
cout<<"Enter the number:\n";
cin>>n;
do
{
f=f*n;
n--;
}while(n>0);
cout<<"The factorial valueis:"<<f;
getch();
}

OUTPUT:
Enter the number: 3
The factorial value is: 6

RESULT:
Thus the program for implementation of Code Optimization technique is executed and verified.