The for keyword in Java is used to create a loop that repeatedly executes a block of code a specific number of times. It is commonly used for iterating over arrays and collections.
Table of Contents
- Introduction
forKeyword Syntax- Understanding
for - Examples
- Basic Usage
- Using
forwith Arrays - Using Enhanced
forLoop
- Real-World Use Case
- Conclusion
Introduction
The for loop in Java is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. It is particularly useful when the number of iterations is known beforehand.
for Keyword Syntax
The syntax for the for loop is as follows:
for (initialization; condition; update) {
// code to be executed
}
Example:
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
Understanding for
The for loop consists of three parts:
- Initialization: This part is executed only once, at the beginning. It is typically used to initialize a loop control variable.
- Condition: This part is evaluated before each iteration. If the condition is true, the loop body is executed. If it is false, the loop terminates.
- Update: This part is executed after each iteration of the loop. It is typically used to update the loop control variable.
Key Points:
- All three parts are optional, but the semicolons
;are mandatory. - The loop body can be a single statement or a block of statements enclosed in curly braces
{}.
Examples
Basic Usage
To demonstrate the basic usage of the for keyword, we will print numbers from 0 to 9.
Example
public class ForExample {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
Output:
0
1
2
3
4
5
6
7
8
9
Using for with Arrays
The for loop is often used to iterate over elements in an array.
Example
public class ForArrayExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
Output:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Using Enhanced for Loop
The enhanced for loop, also known as the "for-each" loop, simplifies iteration over arrays and collections.
Example
public class EnhancedForExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.println("Number: " + number);
}
}
}
Output:
Number: 10
Number: 20
Number: 30
Number: 40
Number: 50
Real-World Use Case
Iterating Over a List of Items
In real-world applications, the for loop is useful for iterating over lists of items, such as processing user inputs, handling collections, or performing repetitive tasks.
Example
import java.util.ArrayList;
import java.util.List;
public class ForListExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
for (String fruit : fruits) {
System.out.println("Fruit: " + fruit);
}
}
}
Output:
Fruit: Apple
Fruit: Banana
Fruit: Cherry
Conclusion
The for keyword in Java is used for creating loops that can execute a block of code multiple times. It is especially useful for iterating over arrays and collections. By understanding and using the for loop, you can efficiently manage repetitive tasks and handle collections of data in your Java programs.