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:

Java for loop Diagram

This flowchart visually represents how a for loop works in Java. Each shape and arrow illustrates a specific step in the loop's execution.

  1. Start : The program execution begins here. It's the entry point to the loop.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. Repetition: As long as the condition i <= 5 remains true, steps 4 and 5 keep repeating. This creates the loop cycle.
  7. 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:

  1. Used when number of iterations is known.
  2. For loop is composed of 3 parts:
    • Initialization: runs once before the loop.
    • Condition: checked before every iteration.
    • Update: executed after each iteration.
  3. Executes the loop body as long as the condition is true.
  4. Can be used for counting, printing sequences, traversing arrays, etc.
  5. Loop stops when the condition becomes false.
  6. More compact than while loop for known iteration counts.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar