EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Java for EmSAT Achieve  >  For Loop in Java

For Loop in Java | Java for EmSAT Achieve 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 | Java for EmSAT Achieve is a part of the EmSAT Achieve Course Java for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
60 videos|37 docs|12 tests

Top Courses for EmSAT Achieve

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

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

For Loop in Java | Java for EmSAT Achieve

,

ppt

,

Exam

,

practice quizzes

,

Extra Questions

,

MCQs

,

Previous Year Questions with Solutions

,

For Loop in Java | Java for EmSAT Achieve

,

study material

,

pdf

,

Objective type Questions

,

Summary

,

Semester Notes

,

Sample Paper

,

Important questions

,

video lectures

,

shortcuts and tricks

,

Free

,

For Loop in Java | Java for EmSAT Achieve

,

past year papers

,

mock tests for examination

,

Viva Questions

;