Java का for Loop
In this page:
for (initialization; condition; update) {
// loop body
}
Basic for Loop
एक for loop initialization, continue condition, और update step को एक single header line में pack करता है, इसे natural choice बनाते हुए जब भी आपको पहले से मोटे तौर पर पता हो कि आपको कितनी बार iterate करना है।
उदाहरण: Basic for Loop
// 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 < 3; i++) {
// Print a line to the console
System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Decrementing for Loops
Loop variable को एक high value पर initialize करना और update step के रूप में i-- उपयोग करना loop को पीछे की ओर चलाता है, जो किसी array को इसके आखिरी element से पहले तक process करने जैसे tasks के लिए handy है।
उदाहरण: Decrementing for Loops
// 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 = 5; i > 0; i--) {
// Print a line to the console
System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating with Custom Steps
Update expression i++ तक सीमित नहीं -- i += 2 लिखना दो-दो steps लेता है, आपको हर दूसरा index visit करने देते हुए या आपकी logic को चाहिए किसी custom increment से count करने देते हुए।
उदाहरण: Iterating with Custom Steps
// 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 < 10; i += 2) {
// Print a line to the console
System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over Arrays
चूंकि array indices 0 से length-1 तक चलती हैं, for (int i = 0; i < arr.length; i++) जैसा एक for loop किसी array के हर element को order में visit और process करने का standard, idiomatic तरीका है।
उदाहरण: Iterating over Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare arr and set it to {10, 20, 30}
int[] arr = {10, 20, 30};
// Loop: repeat while the condition holds
for (int i = 0; i < arr.length; i++) {
// Print a line to the console
System.out.println(arr[i]);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Nested for Loops
एक inner for loop अपने enclosing outer loop की हर single iteration के लिए पूरा चलता है -- दोनों loops हर एक n बार चलते हुए, वह कुल n squared iterations है, 2D grids process करने या किसी list में सभी pairs compare करने के पीछे classic pattern।
उदाहरण: Nested for Loops
// 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 < 2; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 2; j++) {
// Print a line to the console
System.out.println(i + "," + j);
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
array.lengthके साथ<=उपयोग करना, जोArrayIndexOutOfBoundsExceptionका कारण बनता है।for (...)के बाद एक semicolon रखना, जो loop body को अलग करता है।- Body के अंदर loop variable बदलना, जो यह बदलता है कि यह कितनी बार चलता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: