- जब तक किसी condition का satisfaction नहीं होता तब तक Loop का statement repeat होता रहता है |
- while loop
- do-while loop
- for loop
- nested loop
1.While Loop
- While Loop में variable को initialize करना जरुरी है
- अगर While Loop को repeat करना हो तो, increment/decrement operator का इस्तेमाल किया जाता है
- जबतक While Loop में Condition true होती तब तक repeat होता रहता है |
Syntax for While Loop
variable initialization ;while(condition){
statements;
variable increment/decrement;
}
for eg.
while(i<10){
cout<<i<<endl; // statement of while loop
i++; //increment operator
}
Example for While Loop
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int i=0; // Variable initialization
while(i<10) // condition of while loop
{
cout<<i<<endl; // statement of while loop
i++; //increment operator
}
return 0;
}
Output :
0 1 2 3 4 5 6 7 8 9
2.Do-While Loop
- जबतक Do-While Loop में Condition true होती तब तक repeat होता रहता है |
- Do-While की खासियत ये है कि, अगर condition false भी हो तो ये एक statement को output में print करता है |
Syntax for Do-While Loop
do{statements;
}while(condition);
for eg.
do{
cout<<i<<endl;
i++;
}while(i<10);
Example for Do-While Loop
Source Code :
#include <iostream.h>
using namespace std;
int main()
{
int i=0; // Variable initialization
do{
cout<<i<<endl;
i++;
}while(i<10);
return 0;
}
Output :
0 1 2 3 4 5 6 7 8 9
3.For Loop
- For Loop को सिर्फ Variable Declaration कि जरुरत होती है | ये statement छोड़के सभी काम अपने अंदर ही करता है |
- जबतक While Loop में Condition true होती तब तक repeat होता रहता है |
Syntax for For Loop
for( variable_initialization; condition; increment/decrement){statements;
}
for eg.
for( i=0; i<10; i++){
cout<<i<<endl; // statement of while loop
}
Example for For Loop
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int i;
for( i=0; i<10; i++){
cout<<i<<endl; // statement of while loop
}
return 0;
}
Output :
0 1 2 3 4 5 6 7 8 9
4.Nested Loop
- Nested Loop ये loop का कोई प्रकार नहीं है |
- Nested Loop में एक loop में दूसरा loop लिया जाता है |
- Nested Loop While, Do-While, For Loop के होते है |
Example for Nested Loop
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int i,j;
for(i=1;i<4;i++){
for(j=1;j<4;j++){
cout<<i<<"\t"<<j<<endl;
}
}
return 0;
}
Output :
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3







No comments: