For Loop in Java


The for loop in Java is an entry-controlled loop. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The three components of the for loop (separated by ;) are variable declaration/initialization (here int i = 0), the condition (here i < 100), and the increment statement (here i++).

The variable declaration is done once as if placed just inside the { on the first run. Then the condition is checked, if it is true the body of the loop will execute, if it is false the loop will stop. Assuming the loop continues, the body will execute and finally when the } is reached the increment statement will execute just before the condition is checked again.

The curly braces are optional (you can one line with a semicolon) if the loop contains just one statement. But, it's always recommended to use braces to avoid misunderstandings and bugs. The for loop components are optional. If your business logic contains one of these parts, you can omit the corresponding component from your for loop.

Syntax:
   for( initial ; condition ; increment / decrement)
   {
        // body of loop;
   }

This Java program prompts the user to input a number and then prints out all the integers from 1 up to that number using a for loop. Here is a breakdown of the code:

  • The program starts by importing the Scanner class to allow user input.
  • The class is defined as for_loop and the main method is declared.
  • The program prompts the user to enter a limit and waits for the user to input a number.
  • The input number is stored in the variable n.
  • The for loop is defined with three parameters: initialization, condition, and increment.
  • The initialization parameter initializes the loop variable i to 1.
  • The condition parameter sets the loop to continue running as long as the value of i is less than or equal to the value of n.
  • The increment parameter adds 1 to the value of i at the end of each iteration of the loop using the shorthand i++ notation.
  • Inside the for loop, the program prints out the value of i using the println method.
  • The loop continues to run until the condition i<=n is no longer true.
  • Once the loop is finished, the program terminates.

This program is a simple example of how to use a for loop in Java to iterate over a range of numbers. The for loop is used here because we know the starting and ending values of the range we want to iterate over, and we want to increment the loop variable by a fixed amount (in this case, 1).

Source Code

import java.util.Scanner;
 
public class for_loop {
    public static void main(String args[])
    {
        System.out.println("Enter The Limit : ");
        Scanner in =new Scanner(System.in);
        int n=in.nextInt();
        for(int i=1;i<=n;i++)
        {
            System.out.println(i);
        }
    }
}
 
 

Output

Enter The Limit :
10
 
1
2
3
4
5
6
7
8
9
10
To download raw file Click Here

Basic Programs