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

Java का do-while Loop

एक do-while loop पहले steps करता है और बाद में जांचता है, ज़्यादा salt डालने का decide करने से पहले एक dish taste करने जैसा। यह हमेशा कम से कम एक बार चलता है।
Syntax
java
do {
    // runs at least once
} while (condition);

Basic do-while Loop

एक do-while loop पहले अपना body चलाता है और सिर्फ बाद में, आखिर में, condition जांचता है -- एक regular while loop से विपरीत order, जो body में enter होने से पहले condition test करता है।

उदाहरण: Basic do-while Loop

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare i and set it to 0
		int i = 0;
		// do-while: run the body once, then repeat while the condition is true
		do {
			// Print a line to the console
			System.out.println(i);
			i++;
		} while (i < 3);
	}
}

Guaranteeing One Run

यह ordering गारंटी देता है कि loop body कम से कम एक बार execute होता है भले ही condition बिल्कुल शुरुआत से false होती, जो एक standard while loop कभी गारंटी नहीं दे सकता।

उदाहरण: Guaranteeing One Run

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare i and set it to 10
		int i = 10;
		// do-while: run the body once, then repeat while the condition is true
		do {
			// Print a line to the console
			System.out.println("Runs once: " + i);
		} while (i < 5);
	}
}

Incrementing Variables inside Loop

आपको अपने loop condition में जांचे गए variable को do-while block के अंदर कहीं modify करना चाहिए, नहीं तो loop हमेशा के लिए repeat करता रहेगा क्योंकि condition को कभी false होने का मौका नहीं मिलता।

उदाहरण: Incrementing Variables inside Loop

java
public class Main {
	public static void main(String[] args) {
		int i = 0;
		do {
			System.out.println(i);
			i++; // without this, the loop never ends
		} while (i < 3);
	}
}

Simulated Interactive Runs

किसी भी loop की तरह, आपको condition निर्भर करने वाले variable को do-while body के अंदर कहीं update करना चाहिए -- अगर वह variable कभी नहीं बदलता, loop शुरू होने पर हमेशा के लिए चलेगा।

उदाहरण: Simulated Interactive Runs

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare attempts and set it to 0
		int attempts = 0;
		// do-while: run the body once, then repeat while the condition is true
		do {
			attempts++;
			// Print a line to the console
			System.out.println("Attempt " + attempts);
		} while (attempts < 3);
	}
}

Nested do-while Loops

एक do-while को दूसरे do-while के अंदर nest करना आपको एक grid जैसी structures बनाने देता है जहां हर outer iteration हमेशा inner loop का कम से कम एक पूरा pass process करता है, guaranteed।

उदाहरण: Nested do-while Loops

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare row and set it to 1
		int row = 1;
		// do-while: run the body once, then repeat while the condition is true
		do {
			// Declare col and set it to 1
			int col = 1;
			// do-while: run the body once, then repeat while the condition is true
			do {
				// Print to the console without a newline
				System.out.print(col + " ");
				col++;
			} while (col <= 3);
			// Print a line to the console
			System.out.println();
			row++;
		} while (row <= 2);
	}
}
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. एक do-while में while (condition) के बाद semicolon भूल जाना, जो एक compile error है।
  2. Condition false होने पर body skip होने की उम्मीद करना, जब do-while हमेशा एक बार चलता है।
  3. Body के अंदर एक variable declare करना और इसे while condition में उपयोग करना, जहां यह scope से बाहर है।

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.