for loop

A for loop is an entry control loop which has all three parts of loop structure i.e.. initialization, condition and updation. these three parts are separated by semicolon (;) . loop statements are executed til condition is true. when condition is fail then program control will be shifted to next instruction of program. for loop is more effective when programmer already known that how many times loop will be executed. for loop is a simple loop structure and if we remove initialization and updation parts from it then it works similar to while loop.
फॉर लूप एक एंट्री कंट्रोल लूप है इसमें लूप के तीनो भागो इनिशियलाइजेशन, कंडीशन एवं अपडेशन को एक साथ रखा जाता है इन्हें ; अर्धविराम से पृथक किया जाता है। जब तक कंडीशन सत्य होती है लूप के स्टेटमेंट रन होते रहते है अन्यथा प्रोग्राम कंट्रोल अगले स्टेटमेंट पर पहुँच जाता है। इसका प्रयोग तब अत्यधिक प्रभावी होता है जब प्रोग्रामर को यह पहले से ज्ञात हो की लूप स्टेटमेंट को कितनी बार रन किया जायेगा। फॉर लूप एक सिंपल लूप है अर्थात यदि इसमें से इनिशियलाइजेशन एवं अपडेशन भाग को हटा दिया जाता है तब यह व्हाइल लूप के सामान व्यव्हार करता है।

Syntax:- 

for(initialization, condition, updation){
statements;
.................
}

Example:-
1.) C++ program to print table of number N using for loop.

#include<iostream.h>
#include<conio.h>
void main(){
int n;
clrscr();
cout<<"Enter a Number"<<endl;
cin>>n;
cout<<"Table of "<<n<<endl;
for(i=1;i<=10;i++){
cout<<n<<" x "<<i<<" = "<<n*i<<endl;
}
getch();
}

2.) Cpp program for counting from 1 to 100 using for loop.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(){
clrscr();
cout<<"Counting from 1 to 100"<<endl;
for(int i=1;i<=100;i++){
cout<<setw(3)<<i<<" ";
if(i%10==0) cout<<endl;
}
getch();
}



No comments:

Post a Comment