0% found this document useful (0 votes)
7 views1 page

Java For Loop Notes

The 'for' loop in Java is designed to execute a block of code a specific number of times when the number of iterations is known. It consists of three parts: initialization, condition, and update. Examples include printing numbers from 1 to 5 and calculating the sum of the first 10 natural numbers.

Uploaded by

Lalitha yamini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Java For Loop Notes

The 'for' loop in Java is designed to execute a block of code a specific number of times when the number of iterations is known. It consists of three parts: initialization, condition, and update. Examples include printing numbers from 1 to 5 and calculating the sum of the first 10 natural numbers.

Uploaded by

Lalitha yamini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Java Loop: for Loop

1. Definition
The 'for' loop in Java is used to execute a block of code a specific number of times. It is best
used when the number of iterations is known in advance.

2. Syntax
for (initialization; condition; update) {
// Code to be executed
}

3. Explanation
• Initialization: Initializes the loop counter variable.
• Condition: Evaluated before each iteration. If true, the loop runs.
• Update: Changes the loop variable after each iteration.

4. Example 1: Print numbers from 1 to 5


public class ForLoopExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}

5. Example 2: Sum of first 10 natural numbers


public class ForLoopExample2 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
}
}

You might also like