← Back to Core Java Course | Chapter 4: Control Flow | Lesson 9 of 9

Java में break और continue

break तुरंत एक loop छोड़ता है, जीतने के बाद एक game से बाहर चलने जैसा, और continue सिर्फ current round skip करता है। Labels आपको चुनने देते हैं कि कौन सा loop छोड़ना है।
Syntax
java
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

java
// 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);
		}
	}
}

The continue Statement

continue सिर्फ current iteration के बचे code को skip करता है, फिर सीधे loop के अगले condition check या update step पर jump करता है -- break के विपरीत, loop खुद बाद में चलता रहता है।

उदाहरण: The continue Statement

java
// 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);
		}
	}
}

Labeled break

एक labeled break (जैसे outer: for (...) { for (...) { break outer; } }) एक nested inner loop के अंदर code को सीधे एक specific outer loop terminate करने देता है, जो एक plain break -- जो सिर्फ अपने immediate loop को प्रभावित करता है -- नहीं कर सकता।

उदाहरण: Labeled break

java
// 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);
			}
		}
	}
}

Labeled continue

एक labeled continue वही तरीके से काम करता है लेकिन inner loop के बजाय named outer loop की अगली iteration पर skip करता है, आपको current inner-loop pass छोड़ने और outer loop का cycle resume करने देते हुए।

उदाहरण: Labeled continue

java
// 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);
			}
		}
	}
}

break in switch-case

एक switch-case के अंदर, break execution को अगले case block में fall through होने से रोकता है -- functionally यह एक loop से exit होने जैसा ही है, लेकिन यहां यह इसके बजाय switch statement से exit हो रहा है।

उदाहरण: break in switch-case

java
// 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");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक loop के अंदर एक switch के अंदर break उपयोग करना और loop रुकने की उम्मीद करना, जब यह सिर्फ switch से exit होता है।
  2. उसी block में break या continue के बाद code रखना, जो unreachable है और एक compile error है।
  3. पहले counter update किए बिना एक while loop में continue उपयोग करना, जो एक infinite loop बनाता है।
चैप्टर सारांश
  • if, if-else, else-if ladders, और switch conditions के आधार पर चुनते हैं कौन सा code चले।
  • while, do-while, for, और for-each loops code दोहराते हैं, और for-each collections और arrays के लिए convenient है।
  • break और continue control करते हैं कि एक loop कैसे exit होता है या आगे बढ़ता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.