If you’ve been diving into Java, you’ve probably faced the need to iterate over collections of data or repeat actions in your code. Loops are a very big part of programming and having a great understanding of it would be a great help in your journey as a programmer.
Types of Loops in Java:
1. For Loop
2. While Loop
3. Do_While Loop
4. Enhanced For Loop (For-each Loop)
For Loop:
We mainly use for loop these purposes:
Iteration → Iteration means performing the same operation/action on different items, one item at a time. For example: if we have a list and want to print it in the console. It is useful for traversing through collections such as arrays, lists, or other data structures.
Repetition → Repetition is executing a block of code multiple times based on certain conditions or a specific range of values.
When to use for loop?
When we know how many times a block of code should execute, and the number of iterations for the loop is fixed.
The for-loop syntax has three important parts:
1. Initialization → In this part we initialization the block variable
2. Test Condition → The for loop keeps executing until the condition is true
3. Update (Increment/decrement) –> Modifies the loop control variable
Let's go over some examples:
// For loop that starts with i = 1 and continues as long as i <= 10
for (int i = 1; i <= 10; i++) {
System . out . println ( "The value of i is: "+ i) ; // Print the current value of i
}
Output:
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
The value of i is: 5
The value of i is: 6
The value of i is: 7
The value of i is: 8
The value of i is: 9
The value of i is: 10
Here is the breakdown: for ( i = from number 1 loop starts; i <=10 ( i want to run it till the value is equal or less than 10; and then after every value of i,i++ increment the value)
Syntax of For Loop: For (initialization; condition; inc/dec) {code}
In some cases, you might want to run the loop based on the element available in the collection.
Example of iterating over an array using traditional for loop:
Here is the breakdown: for ( i = from number 0 loop starts; i < intArray . length (iterate based on the length of the array to print all array elements; and then after every value of i; the value of i is incremented)
As you can see in the table below: this loop will run five times starting from value 0 to 4.
index 0 has a value of 2
index 1 has a value of 5
index 2 has a value of 6
index 3 has a value of 1
index 4 has a value of 3
Also, we can use enhanced for loop:
When you want clean, simple iteration over elements without needing the index.
Example:
While loop:
When to use While loop?
When we don’t know the number of iterations beforehand and depend on a condition that needs to be checked before each iteration
How it works: Condition keeps checking before each iteration of the loop.If the condition is true, the code inside the loop is executed. If it’s false, the loop terminates and the program continues with the code after the loop.
Syntax of While Loop: While (condition) {code}
Example:
int count = 0;
while (count < 5) {
System .out. println ("Count is: " + count);
count++;
}
Output:
Count is: 0
Count is: 1
Count is: 2
Count is: 3
Count is: 4
While (condition) {code}
Do-While Loop:
The do-while loop is similar to the while loop, but only one thing is different: it guarantees that the loop’s code block will be executed at least once, regardless of whether the condition is true or false at the start.
How It Works: The code inside the loop runs first, and then the condition is checked. If the condition is true, the loop continues. if it’s false, the loop stops.
Syntax of Do-While Loop:
Do {code} while (condition) ;
Example:
public static void main (String [] args) {
int i = 1; // Initialize the loop control variable
do {
System.out.println (i); // Print the current value of i
i++; // Increment the loop control variable
} while (i <= 3); // Condition to check
}
Output:
1
2
3
To wrap things up, managing repetitive tasks and iterating over data efficiently is possible with the help of Loops. You can write cleaner, more efficient code by using for, while, do-while, and enhanced for loops and make your programs robust. Explore different use cases for these loops to see how they can optimize your code and tackle real-world problems effectively.
Comments