Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

Java For Loop: Iterating and Repeating Code

In Java, the for loop is used to iterate over a range of values and execute a code block multiple times. It provides a concise way to perform repetitive tasks by combining the initialization, condition, and iteration steps into a single line of code. The for loop is commonly used when you know the exact number of iterations or when you want to iterate over a collection of elements. In this article, we will explore the for loop in Java and provide examples to help you understand its usage.

The for Loop Syntax

The syntax of the for loop in Java is as follows:

for (initialization; condition; iteration) {
    // code to be executed
}

The initialization step is executed once before the loop begins and is used to initialize the loop variable. The condition is evaluated before each iteration, and if it evaluates to true, the code block within the for loop is executed. The iteration step is executed after each iteration and is used to update the loop variable. After the loop block is executed, the condition is evaluated again, and the process continues until the condition becomes false.

Example: Counting Numbers

Let's consider an example where we want to count from 1 to 5 using a for loop:

for (int count = 1; count <= 5; count++) {
    System.out.println(count);
}

In the above code, the for loop initializes the count variable to 1, checks if count is less than or equal to 5, executes the code block, and increments count by 1 after each iteration. The output of the above code will be:

1
2
3
4
5

Example: Iterating Over an Array

The for loop is often used to iterate over elements in an array or a collection. Here's an example:

int[] numbers = {1, 2, 3, 4, 5};

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

In the above code, the for loop iterates over each element in the numbers array. The loop variable i is used as the index to access each element, which is then printed. The output will be:

1
2
3
4
5

Conclusion

The for loop in Java provides a powerful mechanism for iterating and repeating code. In this article, we explored the syntax of the for loop and provided examples to demonstrate its usage. We saw how to count numbers and iterate over arrays using a for loop. By effectively utilizing the for loop, you can streamline your code and automate repetitive tasks. Continuously practice using for loops and explore more advanced topics, such as nested loops and loop control statements, to enhance your iteration capabilities in Java programming.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners