- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java For Loop
In Java, the for loop is the go-to control flow statement when you need to execute a block of code a specific number of times. Unlike a while loop, which runs as long as a condition is true, a for loop is designed for "definite iteration" meaning you usually know exactly how many times the loop should run before it even starts.
while loop might be a better fit.
Syntax:
for (initialization; condition; update) {
// Code to execute in each iteration
}
The for loop structure is divided into three distinct parts, separated by semicolons, which control the loop's lifecycle:
Initialization:
- Initializes the loop control variable (often called a "counter").
- Executed only once, right before the loop begins.
- Commonly used to declare the variable, like
int i = 0;.
for (int i = 0; ...)), you ensure it doesn't leak into the rest of your program, preventing accidental bugs.
Condition:
- Checked before each iteration of the loop.
- If the expression evaluates to
true, the code block inside the braces runs. - If it evaluates to
false, the loop terminates immediately, and the program continues with the code following the loop.
i >= 0 while i is always increasing), you will create an infinite loop. This will cause your program to hang or crash as it consumes system resources.
Update:
- Modifies the loop control variable after each execution of the loop body.
- Typically used to increment (
i++) or decrement (i--) the counter. - After the update, the program jumps back to the Condition step.
For Loop Example:
- Iterates over a range of values to perform a repetitive task.
- Example: Printing a sequence of numbers.
for (int i = 0; i < 5; i++) {
System.out.println("Iteration " + (i + 1));
}
In this example, the loop starts at 0 and continues as long as i is less than 5. After every print statement, i increases by 1. The result is five printed lines, starting from "Iteration 1" through "Iteration 5".
i < 5 and i <= 5. If you start at 0 and use <= 5, the loop will run 6 times, which might cause an ArrayIndexOutOfBoundsException if you are working with arrays.
Real-World Example: Calculating a Total
Imagine you are building a simple shopping cart application and need to sum the prices of five items:
double[] prices = {19.99, 5.50, 10.00, 2.25, 49.99};
double total = 0;
for (int i = 0; i < prices.length; i++) {
total += prices[i];
}
System.out.println("The total price is: $" + total);
Here, the for loop uses the array's length to ensure every item is processed exactly once, regardless of how many items are in the list.
Summary
The for loop is a powerful construct for iterating over sequences of values or elements in Java. By mastering the initialization, condition, and update phases, you can handle everything from simple counting to complex data processing. Understanding these fundamentals is essential for writing clean, efficient, and bug-free Java applications.