while – loop का execution C ++ में for – loop के समान ही है। लेकिन इसमें up-dation , body of loop का एक हिस्सा होता है, जबकि for – loop में नहीं है।
दोनों loop में, execution एक ही क्रम में होता है। अर्थात पहले variable initialization , फिर condition होती है और अगर condition true होती है तो body of loop execute होता है फिर initialization value update होती है। इस प्रकार एक while – loop execute होता है।
syntax
initialization;
while(condition)
{
body of loop;
updation;
}
एक while – loop flow – diagram निम्नानुसार होगा,
यहाँ while -loop का उदाहरण दिया गया है –
Example of while loop in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num=5;
int i=0;
while(i<num)
{
cout<<i+1<<"statement executed\n"; // body of loop
i++;
}
getch();
}
OUTPUT
1.statement executed
2.statement executed
3.statement executed
4.statement executed
5.statement executed
Explanation
program में, हमने loop का size num = 5 से declared किया है, इसलिए यहां body of loop 5 बार execute होगा।
variable i = 0 दिया गया है, इसके बाद condition चेक होगी जो कि true हो रही है (0 <5) इसलिए body of loop execute हो जायेगा।
आखिरी में, variable i की value 1 से बढ़ जाती है,
इस प्रकार एक while – loop execute होगा।
अब पूरी प्रक्रिया फिर से शुरू होती है जब तक कि condition false नहीं हो जाती . (5 <5) का अर्थ है कि स्थिति फिर से जाँच की जाएगी क्योंकि इस बार value में 1 की वृद्धि हुई है, इसलिए स्थिति (1 <5) होगी और हम जानते हैं कि इस बार की condition भी true हो रही है, इसलिए body of loop फिर execute होगा।
इस program का कार्यन्वन नीचे दी गई तालिका में भी दिया गया है –
initialization value (i=1) | condition check (i<=5) | body of loop executed |
---|---|---|
0 | 0<5 | 1.statement executed |
1 | 1<5 | 2.statement executed |
2 | 2<5 | 3.statement executed |
3 | 3<5 | 4.statement executed |
4 | 4<5 | 5.statement executed |
5 | 5<5 | Exit |
यहाँ पर loop के execution का क्रम निम्नानुसार होगा,
while – loop में निम्नलिखित numbers दोहराए जाएंगे, जब तक कि condition false नहीं हो जाती।
nested while-loop in C++
जब एक while -loop दूसरे while – loop का body होता है, nested while loop कहलाता है। इसके लिए nested for -loop देखें।
syntax
while (expression)
{
// outer-while body
while(expression)
{
// inner-while body
updation;
}
updation;
|
आइए इसे एक उदाहरण के साथ समझते हैं –
nested while loop Progam in loop in C++
कार्यक्रम में outer – loop 3 बार execute होगा हुए जबकि inner – loop 2 बार execute होगा।
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1,j;
while(i<=3)
{
//outer-while body
cout<<i<<"outer-while loop\n";
j=1;
while(j<=2)
{
cout<<"\t"<<j<<"inner-while loop\n";
j++;
}
i++;
}
getch();
}
OUTPUT
1.outer-while loop
1.inner-while loop
2.inner-while loop
2.outer-while loop
1.inner-while loop
2.inner-while loop
3.outer-while loop
1.inner-while loop
2.inner-while loop
Explanation
यहाँ पर पुनरावृत्ति निम्नानुसार होगी,
while-loop with for-loop program in C++
syntax
while (expression)
{
// outer-while body
for(..........)
{
// inner-while body updation;
}
updation;
|
while – loop के साथ for – loop का execution-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1,j;
while(i<=3)
{
//outer-while body
cout<<i<<".while-loop executed\n";
for(j=1; j<=2; j++)
cout<<"\t"<<j<<".for-loop executed\n";
i++;
}
getch();
}
OUTPUT
1.while-loop executed 1.for-loop executed 2.for-loop executed 2.while-loop executed 1.for-loop executed 2.for-loop executed 3.while-loop executed 1.for-loop executed 2.for-loop executed
यहाँ कुछ उदाहरण दिए गए हैं –
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
clrscr();
cout<<"Enter number: ";
cin>>num;
int row=1,col;
while(row<=10)
{
//while-loop body
for(col=1; col<=num; col++)
cout<<row<<"x"<<col<<"="<<row*col<<"\t";// for-loop body
row++;
cout<<endl;
}
getch();
}
OUTPUT
Enter number: 6
1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6
2x1=2 2x2=4 2x3=6 2x4=8 2x5=10 2x6=12
3x1=3 3x2=6 3x3=9 3x4=12 3x5=15 3x6=18
4x1=4 4x2=8 4x3=12 4x4=16 4x5=20 4x6=24
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54
10x1=10 10x2=20 10x3=30 10x4=40 10x5=50 10x6=60
while-loop with if-else statement in C++
नीचे दिए गए program में, सम और विषम संख्याओं को अलग-अलग तरीके से print किया गया-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int count=0,even=0,odd=0,size=11;
cout<<"even"<<"\t"<<"odd"<<endl;
while(count<=size)
{
//while body
if(count%2==0) {
cout<<count<<"\t";
even+=count; //sum of even
}
else {
cout<<count<<"\n";
odd+=count; //sum of odd
}
count++;
}
cout<<"____________\n";
cout<<even<<"\t"<<odd;
getch();
}
OUTPUT
even odd
0 1
2 3
4 5
6 7
8 9
10 11
____________
30 36
for और while लगभग एक जैसे ही हैं। C ++ में एक और loop पाया जाता है जिसे do – while loop कहा जाता है।
Related Exercise