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

Java का while Loop

एक while loop steps को तब तक दोहराता है जब तक एक condition true रहे, soup को गरम होने तक हिलाने जैसा। यह हर round से पहले condition जांचता है।
Syntax
java
while (condition) {
    // loop body
    // update variable
}

Simple while Loop

एक while loop body को एक बार भी चलाने से पहले तुरंत अपना condition जांचता है -- अगर condition शुरुआत में false हो, loop body बिल्कुल नहीं execute होता।

उदाहरण: Simple 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;
		// Repeat while i < 3 is true
		while (i < 3) {
			// Print a line to the console
			System.out.println(i);
			i++;
		}
	}
}

Decrementing Loop Counters

Loop से हर pass पर एक counter से घटाना, जैसे count--, आपको zero (या किसी भी target) की ओर count down करने देता है बजाय ज़्यादा आम upward counting pattern के।

उदाहरण: Decrementing Loop Counters

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare count and set it to 5
		int count = 5;
		// Repeat while count > 0 is true
		while (count > 0) {
			// Print a line to the console
			System.out.println(count);
			count--;
		}
	}
}

Loops with Boolean Flags

Condition में एक boolean flag variable, जैसे while (running), loop body के अंदर code को खुद उस flag को false सेट करके रुकना कब है यह decide करने देता है, जो तब उपयोगी है जब stopping condition complex या event-driven हो।

उदाहरण: Loops with Boolean Flags

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare running and set it to true
		boolean running = true;
		// Declare i and set it to 0
		int i = 0;
		// Repeat while running is true
		while (running) {
			// Print a line to the console
			System.out.println(i);
			i++;
			// Check whether i == 3
			if (i == 3) {
				running = false;
			}
		}
	}
}

Avoiding Infinite Loops

अगर loop body के अंदर कुछ भी condition निर्भर करने वाली value(s) कभी नहीं बदलता, condition हमेशा के लिए true रहता है और loop indefinitely चलता है -- हमेशा double-check करें कि आपका loop variable असल में update हो रहा है।

उदाहरण: Avoiding Infinite Loops

java
public class Main {
	public static void main(String[] args) {
		int i = 0;
		while (i < 3) {
			System.out.println(i);
			i++; // removing this line would make the loop run forever
		}
	}
}

Mathematical Growth Loops

हर iteration एक loop variable को multiply या divide करना (एक fixed amount जोड़ने या घटाने के बजाय) exponential growth या decay produce करता है, binary search जैसे algorithms के लिए उपयोगी जिन्हें logarithmic-scale steps चाहिए।

उदाहरण: Mathematical Growth Loops

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare value and set it to 1
		int value = 1;
		// Repeat while value < 100 is true
		while (value < 100) {
			// Print a line to the console
			System.out.println(value);
			value *= 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. Loop variable बदलना भूल जाना, जो एक infinite loop बनाता है।
  2. शुरुआत से false condition उपयोग करना, इसलिए loop body कभी नहीं चलता।
  3. Off-by-one errors, जैसे < के बजाय <=, जो एक बार ज़्यादा चलते हैं।

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.