For Loops in Java - Notes
What is a for loop?
A for loop in Java is used to execute a block of code repeatedly for a fixed number of times.
Syntax:
for (initialization; condition; update) {
// code to be executed
}
Parts of a for loop:
1. Initialization - Runs once at the beginning (e.g., int i = 0)
2. Condition - Checked before each iteration (e.g., i < 5)
3. Update - Runs after each iteration (e.g., i++)
Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Hello " + i);
}
Output:
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
What is a for loop?
A for loop in Java is used to execute a block of code repeatedly for a fixed number of times.
Syntax:
for (initialization; condition; update) {
// code to be executed
}
Parts of a for loop:
1. Initialization - Runs once at the beginning (e.g., int i = 0)
2. Condition - Checked before each iteration (e.g., i < 5)
3. Update - Runs after each iteration (e.g., i++)
Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Hello " + i);
}
Output:
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5