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

Java का for Loop

एक for loop steps को एक set संख्या में दोहराता है, counting करते हुए दस jumping jacks करने जैसा। यह start, stopping rule और counting सब एक line में रखता है।
Syntax
java
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

java
// 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);
		}
	}
}

Decrementing for Loops

Loop variable को एक high value पर initialize करना और update step के रूप में i-- उपयोग करना loop को पीछे की ओर चलाता है, जो किसी array को इसके आखिरी element से पहले तक process करने जैसे tasks के लिए handy है।

उदाहरण: Decrementing for Loops

java
// 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);
		}
	}
}

Iterating with Custom Steps

Update expression i++ तक सीमित नहीं -- i += 2 लिखना दो-दो steps लेता है, आपको हर दूसरा index visit करने देते हुए या आपकी logic को चाहिए किसी custom increment से count करने देते हुए।

उदाहरण: Iterating with Custom Steps

java
// 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);
		}
	}
}

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

java
// 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]);
		}
	}
}

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

java
// 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);
			}
		}
	}
}
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. array.length के साथ <= उपयोग करना, जो ArrayIndexOutOfBoundsException का कारण बनता है।
  2. for (...) के बाद एक semicolon रखना, जो loop body को अलग करता है।
  3. Body के अंदर loop variable बदलना, जो यह बदलता है कि यह कितनी बार चलता है।

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.