Looping statements in Java are used to execute a block of code repeatedly as long as a specified condition is met. They are essential for automating repetitive tasks, iterating over data, and dynamically controlling the flow of a program.
Types of Looping Statements in Java:
for
Loop:- What is it?
The for
loop is used when the number of iterations is known in advance. It iterates a specific number of times, with control over the initialization, condition, and increment/decrement in one place.
Syntax:
java
for (initialization; condition; update) {
// Code to be executed
}
How it works:
The loop starts by initializing a variable (e.g., int i = 0
), checks a condition (e.g., i < 5
), and executes the block of code if the condition is true
. After each iteration, the update (e.g., i++
) is performed, and the loop repeats until the condition becomes false
.
Example:
java
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
// Output: Iteration 0, Iteration 1, Iteration 2, Iteration 3, Iteration 4
while
Loop:- What is it?
The while
loop is used when the number of iterations is not known in advance, and the loop continues as long as the given condition is true
.
Syntax:
java
while (condition) {
// Code to be executed
}
How it works:
The condition is checked before the loop begins. If the condition is true
, the loop body is executed. The condition is then checked again before the next iteration. If the condition becomes false
, the loop terminates.
Example:
java
int i = 0;
while (i < 5) {
System.out.println("Iteration: " + i);
i++; // Increment the variable
}
// Output: Iteration 0, Iteration 1, Iteration 2, Iteration 3, Iteration 4
do-while
Loop:- What is it?
The do-while
loop is similar to the while
loop, but the condition is checked after the loop body is executed, ensuring that the loop runs at least once, even if the condition is false
initially.
Syntax:
java
do {
// Code to be executed
} while (condition);
How it works:
The loop body is executed first, and then the condition is checked. If the condition is true
, the loop repeats; if it is false
, the loop stops.
Example:
java
int i = 0;
do {
System.out.println("Iteration: " + i);
i++;
} while (i < 5);
// Output: Iteration 0, Iteration 1, Iteration 2, Iteration 3, Iteration 4
- Enhanced
for
Loop (for-each loop): - What is it?
The enhanced for
loop is used to iterate over arrays or collections. It simplifies iterating over data structures without needing an index or iterator explicitly.
Syntax:
java
for (type element : array) {
// Code to be executed
}
How it works:
This loop iterates through each element of the array or collection, assigning the current element to the loop variable (element
), and executes the block of code for each element.
Example:
java
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
// Output: 1, 2, 3, 4, 5
How Looping Statements Work:
- Initialization (for loop only):
- In a
for
loop, a variable is initialized before the loop begins (e.g.,int i = 0
), setting the starting point of the iteration. - Condition Evaluation:
- The loop condition is evaluated before (in
for
andwhile
) or after (indo-while
) each iteration. If the condition istrue
, the loop body is executed. If it’sfalse
, the loop terminates. - Code Execution:
- Each time the loop condition is true, the block of code inside the loop is executed. The code can perform calculations, modify variables, or display output.
- Update (for loop only):
- In a
for
loop, the update step occurs after each iteration, typically incrementing or decrementing the loop variable. - Repetition:
- The loop continues to execute as long as the condition remains
true
. Once the condition isfalse
, the loop ends, and the program moves to the next line of code after the loop.
Flowchart for a for
Loop:
- Initialization (
i = 0
) - Condition (
i < 5
) - Execute loop body (
System.out.println(i)
) - Update (
i++
) - Repeat until condition is
false
Key Points:
for
loop is used when the number of iterations is known.while
loop is used when the number of iterations is unknown, and the condition is checked before each iteration.do-while
loop guarantees that the loop body is executed at least once, as the condition is checked after the loop body.- Enhanced
for
loop (for-each) simplifies array and collection iteration.
Example Combining Loops:
public class LoopingExample { public static void main(String[] args) { // Using a for loop for (int i = 0; i < 3; i++) { System.out.println("for loop iteration: " + i); }
// Using a while loop
int j = 0;
while (j < 3) {
System.out.println("while loop iteration: " + j);
j++;
}
// Using a do-while loop
int k = 0;
do {
System.out.println("do-while loop iteration: " + k);
k++;
} while (k < 3);
// Using an enhanced for loop
int[] numbers = {1, 2, 3};
for (int num : numbers) {
System.out.println("Enhanced for loop: " + num);
}
}
}
Summary:
- Looping statements are used to repeat a block of code multiple times based on a condition.
- The
for
loop is used when the number of iterations is known, while thewhile
anddo-while
loops are used when the number of iterations is unknown. - The enhanced
for
loop is used for iterating over arrays and collections in a simplified manner.
In conclusion, looping statements are vital in Java programming for performing repetitive tasks, iterating over data, and dynamically controlling the flow of the program based on conditions.