Java Arrays Loop

🔁 Java Arrays Loop

In Java, arrays are often processed using loops. This allows you to access, modify, and iterate through elements efficiently. You can use for, for-each, and while loops with arrays.


1. Using for Loop

Classic way to loop through an array using index.


 

📌 Output:

Index 0: 10
Index 1: 20
Index 2: 30
Index 3: 40
Index 4: 50

2. Using for-each Loop

Simpler way to loop through all elements.


 

📌 Output:

Apple
Banana
Orange

3. Using while Loop

You can also loop through arrays with while.


 

📌 Output:

1
2
3
4
5

4. Using do...while Loop

Executes the loop at least once.


 

📌 Output:

100
200
300

5. Sum of Array Elements


 

📌 Output:

Sum = 150

6. Find Maximum Value in Array


 

📌 Output:

Maximum value = 40

🧠 Summary

Loop TypeUse Case
forKnown indices, flexible
for-eachSimpler, read-only iteration
whileConditional iteration
do...whileExecutes at least once

You may also like...