Java Loops in Hindi (For, While, Do-While, For-Each with Examples)

Table of Contents

Java ek object-oriented programming language hai aur isme loops ek important topic hai jo har student ko achhi tarah se samajhna chahiye.
Loop ka use repetition of code ke liye hota hai. Matlab ek hi code ko baar-baar likhne ki zarurat nahi hoti, loop ke through woh code automatically repeat hota hai.

College exams me “Loops in Java” ek frequently asked question hai, isiliye isko detail me samajhna important hai.

What are Loops in Java?

Loop ek control structure hai jo ek condition ke base par ek code block ko baar-baar execute karta hai.

👉 Simple words me: “Loop ka matlab hai ek kaam ko repeat karna jab tak condition true ho.”

Exam me likhne layak definition:
“A loop is a control structure in Java which allows execution of a block of code repeatedly until a specified condition is true.”

Types of Loops in Java

For Loop in Java

  • For loop ka use tab hota hai jab hume pehle se pata ho ki code kitni baar run karna hai.

  • Ye 3 parts me divided hota hai: initialization, condition, increment/decrement.

Syntax:

 
for(initialization; condition; increment/decrement) { // code to be executed }

 

Example:

 
for(int i=1; i<=5; i++) { System.out.println("Count: " + i); }

Output:

 
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5

While Loop in Java

  • While loop ka use tab hota hai jab iterations ka number unknown ho.

  • Jab tak condition true hai, loop chalega.

Syntax:

 
while(condition) { // code to be executed }

Example:

 
int i = 1; while(i <= 5) { System.out.println("Number: " + i); i++; }

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Do-While Loop in Java

  • Do-while loop me code kam se kam ek baar zaroor execute hota hai, chahe condition false hi kyun na ho.

  • Pehle code chalega, fir condition check hogi.

Syntax:

 
do { // code to be executed } while(condition);

Example:

 
int i = 1; do { System.out.println("Value: " + i); i++; } while(i <= 5);

Output:

 
Value: 1 Value: 2 Value: 3 Value: 4 Value: 5

For-Each Loop in Java

  • For-each loop ka use arrays ya collections ke liye hota hai.

  • Ye simple aur readable hota hai.

Syntax:

 
for(dataType var : arrayName) { // code to be executed }

Example:

 
int[] numbers = {10, 20, 30, 40}; for(int num : numbers) { System.out.println("Number: " + num); }

Output:

 
Number: 10 Number: 20 Number: 30 Number: 40

Difference Between Loops in Java

Loop Type Condition Check Timing Best Use Case
For Loop Before execution Fixed iterations
While Loop Before execution Unknown iterations
Do-While After execution At least one execution required
For-Each N/A (direct iteration) Arrays/Collections

Final Java Program (All Loops Example)

Write Java Program to demonstrate different loops..

				
					// Java Program to demonstrate different loops
public class LoopExample {
    public static void main(String[] args) {
        
        // For Loop
        System.out.println("For Loop:");
        for(int i=1; i<=5; i++) {
            System.out.println("Count: " + i);
        }

        // While Loop
        System.out.println("\nWhile Loop:");
        int j = 1;
        while(j <= 5) {
            System.out.println("Number: " + j);
            j++;
        }

        // Do-While Loop
        System.out.println("\nDo-While Loop:");
        int k = 1;
        do {
            System.out.println("Value: " + k);
            k++;
        } while(k <= 5);

        // For-Each Loop
        System.out.println("\nFor-Each Loop:");
        int[] numbers = {10, 20, 30, 40};
        for(int num : numbers) {
            System.out.println("Number: " + num);
        }
    }
}

				
			

📌 Output of Program

				
					For Loop:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

While Loop:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Do-While Loop:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5

For-Each Loop:
Number: 10
Number: 20
Number: 30
Number: 40

				
			

Conclusion

Java me loops ek essential concept hai jo repetitive code likhne ko easy banata hai.

  • For loop → fixed number of times

  • While loop → unknown number of times

  • Do-while → at least once execute

  • For-each → arrays & collections ke liye

Note:  Exam me agar “Explain Java Loops with example” aata hai to tum definition, syntax, ek example aur output likh kar full marks score kar sakte ho.

FAQ

Q1: What is the main difference between while and do-while loop in Java?
Ans. While loop pehle condition check karta hai, jabki do-while loop pehle execute karta hai fir condition check hoti hai.

Q2: Which loop is best for arrays in Java?
Ans. For-each loop arrays ke liye best hai.

Q3: Can a loop run infinitely?
Ans. Haan, agar condition hamesha true ho to loop infinite chalega.

Subscribe to Ravelsoftech

We request you to subscribe our newsletter for upcoming updates.