Slide 1
Slide 2
Slide 3

For Loop in java programming (V32)

No Comments

For Loop

The for loop is a repetitive control structure that allows you to write an efficient loop that needs to be executed a specific number of times. The for loop is useful when you know the exact number of iterations required. Similar to the while loop, the for loop is also an entry-control loop where the given condition determines execution.

  Syntax:


In Java, a for loop is implemented using three components. Below are the components of the for loop in Java:

  • Initialization: Contains the initial value statement(s) of the loop counter.

  • Boolean expression: Contains the condition that needs to be tested.

  • Body: Contains the statement(s) to be executed repeatedly until the given boolean expression becomes true, and also updates the loop counter.

Flow Diagram


Source Code:

Copied!
public class ForLoop {

   public static void main(String args[]) {

      for(int x = 10; x < 20; x = x + 1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

Result:

Source Code:

Copied!
public class StarSingleLoop {
    public static void main(String[] args) {
        int n = 5; // Number of rows

        for (int i = 1; i <= n * n; i++) {
            System.out.print("* ");
            if (i % n == 0) { //  Break line after every 'n' stars
                System.out.println();
            }
        }
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top