Break Statement in C in Hindi
Break C में एक कीवर्ड है जो प्रोग्राम control को लूप से बाहर लाने के लिए उपयोग किया जाता है। break स्टेटमेंट का उपयोग loop या switch स्टेटमेंट के अंदर किया जाता है। break स्टेटमेंट एक-एक करके loop को तोड़ता है, यानी nested लूप के मामले में, यह पहले inner लूप को तोड़ता है और फिर outer लूप को आगे बढ़ाता है। C में ब्रेक स्टेटमेंट को निम्नलिखित दो scenarios में उपयोग किया जा सकता है:
- With switch case
- With loop
Syntax:
//loop or switch case
break;
Flowchart of break in c
Example
#include<stdio.h>
#include<stdlib.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
Output
0 1 2 3 4 5 came outside of loop i = 5
C break statement with the nested loop
ऐसे मामले में, यह केवल आंतरिक लूप को तोड़ता है, लेकिन बाहरी लूप नहीं।
#include<stdio.h>
int main(){
int i=1,j=1;//initializing a local variable
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
printf("%d &d\n",i,j);
if(i==2 && j==2){
break;//will break loop of j only
}
}//end of for loop
return 0;
}
Output
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
जैसा कि आप कंसोल पर आउटपुट देख सकते हैं, 2 3 मुद्रित नहीं है क्योंकि i == 2 और j == 2 को प्रिंट करने के बाद एक break statement है। लेकिन 3 1, 3 2 और 3 3 मुद्रित होते हैं क्योंकि break स्टेटमेंट का उपयोग केवल inner लूप को तोड़ने के लिए किया जाता है।
break statement with while loop
While loop अंदर के ब्रेक स्टेटमेंट का उपयोग करने के लिए निम्न उदाहरण पर विचार करें।
#include<stdio.h>
void main ()
{
int i = 0;
while(1)
{
printf("%d ",i);
i++;
if(i == 10)
break;
}
printf("came out of while loop");
}
Output
0 1 2 3 4 5 6 7 8 9 came out of while loop
break statement with do-while loop
do-while loop के साथ ब्रेक स्टेटमेंट का उपयोग करने के लिए निम्नलिखित उदाहरण पर विचार करें।
#include<stdio.h>
void main ()
{
int n=2,i,choice;
do
{
i=1;
while(i<=10)
{
printf("%d X %d = %d\n",n,i,n*i);
i++;
}
printf("do you want to continue with the table of %d , enter any non-zero value to continue.",n+1);
scanf("%d",&choice);
if(choice == 0)
{
break;
}
n++;
}while(1);
}
Output
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
do you want to continue with the table of 3 , enter any non-zero value to continue.1
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
क्या आप 4 की table के साथ जारी रखना चाहते हैं, जारी रखने के लिए कोई भी non-zero मान दर्ज करें