Software Development Exam  >  Software Development Notes  >  Basics of Java  >  For Loop in Java

For Loop in Java | Basics of Java - Software Development PDF Download

Introduction

A for loop is a fundamental construct in programming that allows you to repeat a block of code a specific number of times. It's an essential tool for automating repetitive tasks and iterating over collections of data. In Java, the for loop provides a concise and structured way to accomplish these tasks. In this article, we'll dive into the details of the for loop in Java, covering its syntax, usage, and providing multiple examples along the way.

The Syntax of the For Loop

The syntax of a for loop in Java consists of three essential components: initialization, condition, and iteration.

for (initialization; condition; iteration) {

    // Code to be executed

}

  • Initialization: Set an initial value for the loop control variable.
  • Condition: Specify the condition that must be true for the loop to continue executing.
  • Iteration: Define how the loop control variable changes with each iteration.

Executing Code with For Loop

Let's start with a simple example to illustrate the basic usage of a for loop. Suppose we want to print the numbers from 1 to 5.

for (int i = 1; i <= 5; i++) {

    System.out.println(i);

}

Code Explanation:

  • int i = 1;: We initialize the loop control variable i to 1.
  • i <= 5;: The loop continues executing as long as i is less than or equal to 5.
  • i++: After each iteration, i is incremented by 1.
  • System.out.println(i);: Prints the value of i.

Output

1

2

3

4

5

Using For Loop with Arrays and Collections

For loops are often used to iterate over arrays and collections. Let's explore how to use a for loop with an array and a collection of strings.

Example 1: Iterating over an Array

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


for (int i = 0; i < numbers.length; i++) {

    System.out.println(numbers[i]);

}

Code Explanation:

  • int[] numbers = {1, 2, 3, 4, 5};: We create an integer array called numbers.
  • i < numbers.length;: The loop continues executing as long as i is less than the length of the numbers array.
  • System.out.println(numbers[i]);: Prints the value at the i-th index of the numbers array.

Output

1

2

3

4

5

Example 2: Iterating over a Collection

List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");


for (String fruit : fruits) {

    System.out.println(fruit);

}

Code Explanation:

  • List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");: We create a List called fruits and initialize it with strings.
  • for (String fruit : fruits): For each element fruit in the fruits collection, execute the code block.
  • System.out.println(fruit);: Prints the value of fruit.

Output

Apple

Banana

Orange

Controlling Loop Flow with Break and Continue

Java provides the break and continue statements to control the flow of a loop.

Example 1: Using Break

for (int i = 1; i <= 10; i++) {

    if (i == 6) {

        break;

    }

    System.out.println(i);

}

Code Explanation:

  • When i becomes equal to 6, the break statement is executed, and the loop is terminated.

Output

1

2

3

4

5

Example 2: Using Continue

for (int i = 1; i <= 5; i++) {

    if (i == 3) {

        continue;

    }

    System.out.println(i);

}

Code Explanation:

  • When i becomes equal to 3, the continue statement is executed, and the loop skips the remaining code in the current iteration. It then proceeds to the next iteration.

Output

1

2

4

5

Sample Problems and Solutions

Problem 1: Calculate the sum of numbers from 1 to 100.

int sum = 0;


for (int i = 1; i <= 100; i++) {

    sum += i;

}


System.out.println("Sum: " + sum);

Output

Sum: 5050

Problem 2: Print the elements of an array in reverse order.

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


for (int i = numbers.length - 1; i >= 0; i--) {

    System.out.println(numbers[i]);

}

Output

5

4

3

2

1

Conclusion

In this article, we covered the basics of using a for loop in Java. We explored its syntax, demonstrated how to execute code, iterate over arrays and collections, and control the flow of a loop using the break and continue statements. By mastering for loops, you'll have a powerful tool to automate repetitive tasks and efficiently process data in your Java programs.

The document For Loop in Java | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Top Courses for Software Development

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

video lectures

,

Sample Paper

,

practice quizzes

,

Semester Notes

,

Viva Questions

,

MCQs

,

mock tests for examination

,

Important questions

,

past year papers

,

Extra Questions

,

Exam

,

shortcuts and tricks

,

For Loop in Java | Basics of Java - Software Development

,

Free

,

pdf

,

For Loop in Java | Basics of Java - Software Development

,

ppt

,

Objective type Questions

,

Summary

,

For Loop in Java | Basics of Java - Software Development

,

Previous Year Questions with Solutions

,

study material

;