for Loop
The for loop in Java is used when you know in advance how many times you want to execute a block of code.
Syntax:
for(initialization; condition; update) {
// code to be executed
}
Explanation:
- Initialization: A variable is initialized before the loop starts.
- Condition: The loop runs only if this condition is true.
- Update: The variable is updated (increment/decrement) after each iteration.
Example:
class ShikshaSanchar{
public static void main(String[] args){
for(int i = 1; i <= 5; i++) {
System.out.println("Hello from Shiksha Sanchar " + i);
}
}
}
Output:
Hello from Shiksha Sanchar 1
Hello from Shiksha Sanchar 2
Hello from Shiksha Sanchar 3
Hello from Shiksha Sanchar 4
Hello from Shiksha Sanchar 5
Explanation of for loop:
1. Initialization:
int i = 1;
- This part runs only once, before the loop starts.
- It declares a variable i and sets its initial value to 1.
2. Condition Check:
i <= 5;
- Before each loop iteration, Java checks if this condition is true.
- If true, the loop body runs.
- If false, the loop stops.
3. Loop Body:
System.out.println("Hello from Shiksha Sanchar " + i);
- This statement runs only when the condition is true.
- It prints a message with the current value of i.
4. Update Statement:
i++;
- After the loop body executes, i is incremented by 1.
- Then the condition is checked again for the next iteration.
Iteration-wise Flow:
| Iteration | i Value | Condition (i <= 5) | Output |
|---|---|---|---|
| 1 | 1 | true | Hello from Shiksha Sanchar 1 |
| 2 | 2 | true | Hello from Shiksha Sanchar 2 |
| 3 | 3 | true | Hello from Shiksha Sanchar 3 |
| 4 | 4 | true | Hello from Shiksha Sanchar 4 |
| 5 | 5 | true | Hello from Shiksha Sanchar 5 |
| 6 | 6 | false | Loop terminates (execution stops) |
Flow diagram of above program:
This flowchart visually represents how a for loop works in Java. Each shape and arrow illustrates a specific step in the loop's execution.
- Start : The program execution begins here. It's the entry point to the loop.
- Initialization (Initialize i = 1): This is the first step of the for loop. A loop control variable i is initialized to 1. This step executes only once, before the loop starts.
- Condition Check (Is i <= 5?): Before each iteration, the loop checks whether
i is less than or equal to 5. This is a decision-making point.
- Yes: If the condition is true, the loop body runs.
- No: If the condition is false, the loop terminates.
- Loop Body (Print "Hello from Shiksha Sanchar " + i): This is the main logic inside the loop. It prints a message containing the current value of i.
- Update Step (i = i + 1): After printing, the loop variable i is incremented by 1. Then, the flow returns back to the condition check step to re-evaluate.
- Repetition: As long as the condition i <= 5 remains true, steps 4 and 5 keep repeating. This creates the loop cycle.
- End: When the condition becomes false (i.e., i > 5), the loop exits. The control moves to the "End" node. This marks the end of the program or the loop block.
Use Cases of For Loop:
- Repeating a block of code a fixed number of times.
- Traversing arrays or collections.
- Generating patterns (e.g., stars, numbers).
- Performing operations like summing a series of numbers.
Key Points:
- Used when number of iterations is known.
- For loop is composed of 3 parts:
- Initialization: runs once before the loop.
- Condition: checked before every iteration.
- Update: executed after each iteration.
- Executes the loop body as long as the condition is true.
- Can be used for counting, printing sequences, traversing arrays, etc.
- Loop stops when the condition becomes false.
- More compact than while loop for known iteration counts.