C ++ में, Program में statement को छोड़ने के लिए continue statement का उपयोग किया जाता है। C ++ में एक अन्य control statement के साथ इस statement का उपयोग करके, हम program में किसी statement को skip कर सकते हैं।
याद रखें, continue
program को terminate नहीं करता है यह केवल execution में statement को skip करता है।
syntax
{
continue;
.........
}
Flow diagram of continue in C++
नीचे continue – statement का flow -diagram diagram दिया गया है इसे बेहतर तरीके से समझने के लिए आप इसे break के flow – diagram से compare करें –
Example of continue statement in C++
नीचे दिए गए Program में हम number 0 – 10 को print करते हैं, जिसमें हम continue statement का प्रयोग करके number 5 को छोड़ देंगे।
#include<iostream.h>
#include<conio.h>
void main()
{
int num=10;
for(int i = 0; i <= num; i++)
{
if(i==5)
continue;
cout<<"\t"<<i;
}
cout<<"Number 5 is skip";
getch();
}
OUTPUT
0 1 2 3 4 6 7 8 9 10
Number 5 is skip.
Explanation
Program में, continue statement एक if-statement के अंदर defined किया गया है जहाँ एक if-statement खुद एक for-loop के अंदर defined किया गया है।
यहां for -loop 0 से 10. तक print करता है, लेकिन प्रत्येक number print होने से पहले condition की जांच की जाएगी जैसा कि for – loop में होता है और अगर condition true हो जाती है। तो continue statement execute होगा और संख्या 5 वाली statement skip हो जायगी।
यदि यहां, continue का उपयोग नहीं किया गया होता, तो समान output प्राप्त करने के लिए if -else
का उपयोग किया जाता।
निम्नलिखित flow – diagram में इसका execution दिया गया है –
यहाँ एक और program दिया गया है जिसमे हम विषम संख्या को print करते हैं और सम संख्या को skip करते हैं।
Print odd number using continue in C++
नीचे का program ऊपर जैसा ही है। बस केवल यहाँ हम 20 तक विषम संख्याएँ print करेंगे।
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num=20;
for(int i = 0; i <= num; i++)
{
if(i%2==0)
continue; // even number skip
cout<<"\t"<<i; // odd number print
}
getch();
}
OUTPUT
1 3 5 7 9 11 13 15 17 19
Explanation
हर बार if – statement में continue – statement से , विषम संख्या को छोड़ दिया जाएगा (जैसा कि condition दी गई है)।
Print odd number table using continue in C++
नीचे हम केवल विषम संख्या-तालिका print कर रहे हैं, यहाँ हम पंक्ति के रूप में while – loop और स्तंभ के रूप में for – loop का उपयोग करते हैं।
अन्य statements उपर्युक्त programs के समान हैं।
#include<iostream.h>
#include<conio.h>
void main()
{
int num=20;
clrscr();
int row=1,col;
while(row<=10)
{
for(col=1; col<=num; col++)
{
if(col%2==0)
continue;
cout<<row*col<<"\t";
}
row++;
cout<<endl;
}
getch();
}
OUTPUT
1 3 5 7 9 11 13 15 17 19
2 6 10 14 18 22 26 30 34 38
3 9 15 21 27 33 39 45 51 57
4 12 20 28 36 44 52 60 68 76
5 15 25 35 45 55 65 75 85 95
6 18 30 42 54 66 78 90 102 114
7 21 35 49 63 77 91 105 119 133
8 24 40 56 72 88 104 120 136 152
9 27 45 63 81 99 117 135 153 171
10 30 50 70 90 110 130 150 170 190
difference between break and continue statement in C++
break
प्रकार के statement का प्रयोग करके हम किसी statement या set of statements को terminate करते हैं जबकि continue
-statement का प्रयोग program में केवल एक single statement को skip करने के लिए किया जाता है।
previous – break statement in C++
next- goto statement in C++