Java का while Loop
In this page:
while (condition) {
// loop body
// update variable
}
Simple while Loop
एक while loop body को एक बार भी चलाने से पहले तुरंत अपना condition जांचता है -- अगर condition शुरुआत में false हो, loop body बिल्कुल नहीं execute होता।
उदाहरण: Simple while Loop
// 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++;
}
}
}
Login to try C/C++/Java/PHP code in the editor
Decrementing Loop Counters
Loop से हर pass पर एक counter से घटाना, जैसे count--, आपको zero (या किसी भी target) की ओर count down करने देता है बजाय ज़्यादा आम upward counting pattern के।
उदाहरण: Decrementing Loop Counters
// 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--;
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
Avoiding Infinite Loops
अगर loop body के अंदर कुछ भी condition निर्भर करने वाली value(s) कभी नहीं बदलता, condition हमेशा के लिए true रहता है और loop indefinitely चलता है -- हमेशा double-check करें कि आपका loop variable असल में update हो रहा है।
उदाहरण: Avoiding Infinite Loops
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
}
}
}
Login to try C/C++/Java/PHP code in the editor
Mathematical Growth Loops
हर iteration एक loop variable को multiply या divide करना (एक fixed amount जोड़ने या घटाने के बजाय) exponential growth या decay produce करता है, binary search जैसे algorithms के लिए उपयोगी जिन्हें logarithmic-scale steps चाहिए।
उदाहरण: Mathematical Growth Loops
// 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;
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Loop variable बदलना भूल जाना, जो एक infinite loop बनाता है।
- शुरुआत से
falsecondition उपयोग करना, इसलिए loop body कभी नहीं चलता। - Off-by-one errors, जैसे
<के बजाय<=, जो एक बार ज़्यादा चलते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: