- Introduction to Break statement in Java in Hindi
- Java Break Statement with Loop in Hindi
- Java Break Statement with Inner Loop in Hindi
- Java Break Statement with Labeled For Loop
- Java Break Statement in while loop
- Java Break Statement in do-while loop
Introduction to Break statement in Java
जब एक लूप के अंदर एक ब्रेक स्टेटमेंट आ जाती है, तो लूप तुरंत समाप्त हो जाता है और लूप के बाद अगले स्टेटमेंट में प्रोग्राम कंट्रोल फिर से शुरू हो जाता है।
जावा ब्रेक स्टेटमेंट का उपयोग लूप या स्विच स्टेटमेंट को तोड़ने के लिए किया जाता है । यह निर्दिष्ट स्थिति में program के वर्तमान प्रवाह को तोड़ता है। inner लूप के मामले में, यह केवल आंतरिक लूप को तोड़ता है।
हम सभी प्रकार के लूप में जावा ब्रेक स्टेटमेंट का उपयोग कर सकते हैं जैसे कि for loop, while loop और do-while loop में।
Syntax:
jump-statement;
break;

Loop के साथ जावा ब्रेक स्टेटमेंट
उदाहरण:
//Java Program to demonstrate the use of break statement
//inside the for loop.
public class BreakExample {
public static void main(String[] args) {
//using for loop
for(int i=1;i<=10;i++){
if(i==5){
//breaking the loop
break;
}
System.out.println(i);
}
}
}
आउटपुट:
1 2 3 4
Inner लूप के साथ जावा ब्रेक स्टेटमेंट
यह केवल inner लूप को तोड़ता है यदि आप आंतरिक लूप के अंदर break statement का उपयोग करते हैं।
उदाहरण:
//Java Program to illustrate the use of break statement
//inside an inner loop
public class BreakExample2 {
public static void main(String[] args) {
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using break statement inside the inner loop
break;
}
System.out.println(i+" "+j);
}
}
}
}
आउटपुट:
1 1 1 2 1 3 2 1 3 1 3 2 3 3
Labeled for loop के साथ जावा ब्रेक स्टेटमेंट
हम एक लेबल के साथ ब्रेक स्टेटमेंट का उपयोग कर सकते हैं। यह फीचर JDK 1.5 के बाद से पेश किया गया है। तो, हम जावा में किसी भी लूप को break सकते हैं चाहे वह outer लूप हो या inner
उदाहरण:
//Java Program to illustrate the use of continue statement
//with label inside an inner loop to break outer loop
public class BreakExample3 {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using break statement with label
break aa;
}
System.out.println(i+" "+j);
}
}
}
}
आउटपुट:
1 1 1 2 1 3 2 1
While लूप में जावा ब्रेक स्टेटमेंट
उदाहरण:
//Java Program to demonstrate the use of break statement
//inside the while loop.
public class BreakWhileExample {
public static void main(String[] args) {
//while loop
int i=1;
while(i<=10){
if(i==5){
//using break statement
i++;
break;//it will break the loop
}
System.out.println(i);
i++;
}
}
}
आउटपुट:
1 2 3 4
Java break statement in do-while loop
उदाहरण:
//Java Program to demonstrate the use of break statement
//inside the Java do-while loop.
public class BreakDoWhileExample {
public static void main(String[] args) {
//declaring variable
int i=1;
//do-while loop
do{
if(i==5){
//using break statement
i++;
break;//it will break the loop
}
System.out.println(i);
i++;
}while(i<=10);
}
}
आउटपुट:
1 2 3 4