Java Loops
Java Loops
Loops in Java are fundamental constructs used to execute a block of code repeatedly as long as a specified condition is true. They help reduce code redundancy by allowing repetitive tasks to be handled efficiently.
Java provides several types of loops, including:
1. for Loop
The for loop is generally used when the number of iterations is predetermined. It consists of three components: initialization, condition, and update expression.
Syntax:
for (initialization; condition; update) { // Code to execute repeatedly }
Example:
public class ForLoopExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { // Loop runs from 1 to 5 System.out.println("Iteration: " + i); } } }
Output:
Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5
2. while Loop
The while loop is used when the number of iterations is uncertain and depends on a condition being true. It checks the condition before executing the loop body.
Syntax:
while (condition) { // Code to execute repeatedly }
Example:
public class WhileLoopExample { public static void main(String[] args) { int i = 1; while (i <= 5) { // Loop continues while i is less than or equal to 5 System.out.println("Count: " + i); i++; } } }
Output:
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
3. do-while Loop
The do-while loop guarantees at least one execution of the loop body before evaluating the condition.
Syntax:
do { // Code to execute at least once } while (condition);
Example:
public class DoWhileLoopExample { public static void main(String[] args) { int i = 1; do { System.out.println("Number: " + i); i++; } while (i <= 5); // Condition is checked after executing the loop } }
Output:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
4. Enhanced for Loop (or for-each Loop)
The enhanced for loop is specifically designed for iterating through arrays or collections. It simplifies the syntax for traversing elements.
Syntax:
for (dataType element : collection) { // Code to execute for each element }
Example:
public class ForEachLoopExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; for (int num : numbers) { // Iterates over each element in the array System.out.println("Value: " + num); } } }
Output:
Value: 1 Value: 2 Value: 3 Value: 4 Value: 5
5. Nested Loops
Java allows loops to be nested, meaning a loop inside another loop. They are useful for working with multi-dimensional data structures.
Syntax:
for (initialization; condition; update) { for (initialization; condition; update) { // Code to execute in nested loop } }
Example:
public class NestedLoopExample { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { // Outer loop for (int j = 1; j <= 2; j++) { // Inner loop System.out.println("i: " + i + ", j: " + j); } } } }
Output:
i: 1, j: 1 i: 1, j: 2 i: 2, j: 1 i: 2, j: 2 i: 3, j: 1 i: 3, j: 2
6. break and continue Statements in Loops
- break: Exits the loop immediately.
- continue: Skips the current iteration and moves to the next iteration.
Java allows loops to be nested, meaning a loop inside another loop. They are useful for working with multi-dimensional data structures.
Example with break:
public class BreakExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { break; // Terminates the loop when i is 3 } System.out.println("Value: " + i); } } }
Output:
Value: 1 Value: 2
Example with continue:
public class ContinueExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skips the iteration when i is 3 } System.out.println("Value: " + i); } } }
Output:
Value: 1 Value: 2 Value: 4 Value: 5
Conclusion
Loops are essential in Java for handling repetitive tasks. The choice of loop depends on the scenario:
- Use for when the number of iterations is fixed.
- Use while for conditions that need to be evaluated before execution.
- Use do-while for conditions that require execution at least once.
- Use enhanced for for working with arrays or collections.