break statement or break keyword

In C++ programming language break statement is used to perform following two tasks -
C++ प्रोग्रामिंग में ब्रेक स्टेटमेंट का प्रयोग निम्न दो कार्यो को पूर्ण करने के लिए किया  है-

1.) When a break statement is occurred inside a loop, the loop is immediately terminated and the program control will be shifted to the next statement of program.
यदि ब्रेक स्टेटमेंट का प्रयोग लूप के अंतर्गत किया जाता है तब यह उस सम्बंधित लूप को तत्काल समाप्त  कर देता है एवं प्रोग्राम कंट्रोल को प्रोग्राम के अगले स्टेटमेंट पर शिफ्ट करता है।   
2.)when a break statement is occurred inside a switch case then It terminates a case in the switch statement. in absence of break statement all below cases will be executed of switch statement.
यदि ब्रेक स्टेटमेंट का प्रयोग स्विच केस के अंतर्गत किया जाता है तब यह स्विच केस स्टेटमेंट को समाप्त कर देता है यदि ब्रेक स्टेटमेंट अनुपस्थित हो तब निचे लिखे हुए केस भी रन हो जाते है। 

Syntax:- break;

Example:-
1.) C++ program for breaking a loop.
#include<iostream.h>
void main(){
int i;
cout<<"Counting  from 1 to 10"<<endl;
for(i=1;i<=10;i++){
if(i==5) break;
cout<<i<<" ";
}
}

output:- 1 2 3 4

2.) C++ program for breaking case of switch.

#include<iostream.h>
#include<conio.h>
void main(){
int day;
clrscr();
cout<<"Enter Day between 1 to 7 "<<endl;
cin>>day;
switch(day){
case 1:cout<<"Monday"<<endl;break;
case 2:cout<<"Tuesday"<<endl;break;
case 3:cout<<"Wednesday"<<endl;break;
case 4:cout<<"Thursday"<<endl;break;
case 5:cout<<"Friday"<<endl;break;
case 6:cout<<"Saturday"<<endl;break;
case 7:cout<<"Sunday"<<endl;break;
default:cout<<"Wrong Month"<<endl;
}
getch();
}

No comments:

Post a Comment