Java में break और continue
In this page:
for (initialization; condition; update) {
if (condition1) break; // exit loop
if (condition2) continue; // next iteration
}
The break Statement
break तुरंत अपने enclosing loop से exit करता है, किसी भी बची iterations को पूरी तरह skip करते हुए और loop के closing brace के बाद पहले statement पर सीधे jump करते हुए।
उदाहरण: The break Statement
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 0; i < 5; i++) {
// Check whether i == 3
if (i == 3) {
// Leave the switch/loop
break;
}
// Print a line to the console
System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
The continue Statement
continue सिर्फ current iteration के बचे code को skip करता है, फिर सीधे loop के अगले condition check या update step पर jump करता है -- break के विपरीत, loop खुद बाद में चलता रहता है।
उदाहरण: The continue Statement
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 0; i < 5; i++) {
// Check whether i == 2
if (i == 2) {
// Skip the rest of this iteration
continue;
}
// Print a line to the console
System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Labeled break
एक labeled break (जैसे outer: for (...) { for (...) { break outer; } }) एक nested inner loop के अंदर code को सीधे एक specific outer loop terminate करने देता है, जो एक plain break -- जो सिर्फ अपने immediate loop को प्रभावित करता है -- नहीं कर सकता।
उदाहरण: Labeled break
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether j == 1
if (j == 1) {
break outer;
}
// Print a line to the console
System.out.println(i + "," + j);
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
Labeled continue
एक labeled continue वही तरीके से काम करता है लेकिन inner loop के बजाय named outer loop की अगली iteration पर skip करता है, आपको current inner-loop pass छोड़ने और outer loop का cycle resume करने देते हुए।
उदाहरण: Labeled continue
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether j == 1
if (j == 1) {
continue outer;
}
// Print a line to the console
System.out.println(i + "," + j);
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
break in switch-case
एक switch-case के अंदर, break execution को अगले case block में fall through होने से रोकता है -- functionally यह एक loop से exit होने जैसा ही है, लेकिन यहां यह इसके बजाय switch statement से exit हो रहा है।
उदाहरण: break in switch-case
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare day and set it to 2
int day = 2;
// Choose a branch based on the value of day
switch (day) {
case 1:
System.out.println("Monday");
// Leave the switch/loop
break;
case 2:
System.out.println("Tuesday");
// Leave the switch/loop
break;
// default runs when no case matches
default:
System.out.println("Other");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- एक loop के अंदर एक
switchके अंदरbreakउपयोग करना और loop रुकने की उम्मीद करना, जब यह सिर्फ switch से exit होता है। - उसी block में
breakयाcontinueके बाद code रखना, जो unreachable है और एक compile error है। - पहले counter update किए बिना एक
whileloop मेंcontinueउपयोग करना, जो एक infinite loop बनाता है।
if,if-else, else-if ladders, औरswitchconditions के आधार पर चुनते हैं कौन सा code चले।while,do-while,for, और for-each loops code दोहराते हैं, और for-each collections और arrays के लिए convenient है।breakऔरcontinuecontrol करते हैं कि एक loop कैसे exit होता है या आगे बढ़ता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: