Java for loop
What is a For Loop?
A for loop has three main parts:
- Initialization: This is where you set a starting value for a counter variable.
- Termination Condition: The loop runs as long as this condition is true.
- Iteration Expression: After each loop cycle, this updates the counter variable.
Why Are Loops Important in Programming?
Using loops is important as they allow you to run code times without the need to rewrite it repeatedly. This comes in handy for tasks that involve doing things over again such as handling each element in a list or carrying out recurring computations.
Basic Syntax of Java For Loop
In Java, the basic syntax of a for loop looks like this:
- Initialization: Sets the loop counter (e.g.,
int i = 0
). - Condition: The loop runs as long as this is true (e.g.,
i < 10
). - Iteration: Updates the counter (e.g.,
i++
).
Structure of a For Loop
The for loop structure in Java is:
- Initialization Statement: Runs once at the start.
- Termination Condition: Checked before each iteration; if false, the loop ends.
- Increment/Update Statement: Updates the loop counter after each cycle.
Parts of a For Loop
A for loop in Java has three key parts:
- Initialization: Sets the starting value for the loop variable.
- Termination Condition: Determines when the loop should stop.
- Increment/Decrement: Changes the loop variable after each iteration.
Using a For Loop to Iterate Over an Array
To iterate over an array using a for loop:
- Define the array, e.g.,
int[] numbers = {1, 2, 3, 4, 5};
. - Write the for loop, e.g.,
for(int i = 0; i < numbers.length; i++)
. - Access array elements using the loop variable, e.g.,
System.out.println(numbers[i]);
.
Accessing Elements in Arrays Using a For Loop
To access elements in an array using a for loop:
- Declare the loop with the appropriate index variable.
- Use the index to access array elements, e.g.,
numbers[i]
. - Perform any desired operations on each element.
Modifying Elements in Arrays Using a For Loop
To modify elements in an array:
- Create the array and initialize it.
- Use a for loop to iterate through the array.
- Modify each element as needed within the loop.
For example, to increment each element:
Comparison Between Different Types of Loops in Java
Java offers three main loop types:
- For Loop: Best when the number of iterations is known.
- While Loop: Ideal when the number of iterations is unknown, and it runs as long as a condition is true.
- Do-While Loop: Similar to a while loop but guarantees at least one execution of the loop body.
Each loop type has its own strengths depending on the situation.
Master Java by choosing your ideal learning course
View all courses
Introduction to Java
4.4
Understand the language behind millions of apps. Grasp core concepts, write basic programs, and make your first step toward breaking into tech.
8 projects
87K already learning
Java Backend Developer
4.6
Designed for the committed. Master Java Backend development with Spring Boot to build strong web apps and unlock high-demand job opportunities.
27 projects
37K already learning