Java break and continue
Explanation (English): In Java loops, break
is used to exit the loop immediately, while continue
skips the current iteration and continues with the next iteration of the loop.
व्याख्या (हिंदी): Java के लूप में, break
का उपयोग लूप को तुरंत बंद करने के लिए किया जाता है, जबकि continue
वर्तमान चक्र को छोड़कर अगले चक्र पर जाता है।
Break Statement Example
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // exit loop when i == 5
}
System.out.println(i);
}
}
}
Explanation: Loop prints numbers from 1 to 4 and exits when i == 5
.
व्याख्या: लूप 1 से 4 तक संख्या प्रिंट करता है और जब i == 5
होता है तो बंद हो जाता है।
Continue Statement Example
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // skip iteration when i == 3
}
System.out.println(i);
}
}
}
Explanation: When i == 3
, the iteration is skipped, so it prints 1, 2, 4, 5.
व्याख्या: जब i == 3
होता है, उस चक्र को छोड़ दिया जाता है, इसलिए 1, 2, 4, 5 प्रिंट होंगे।