Share on Google+Share on Google+

For Loop in Java

In this section you will learn how to use the For-Loop statement in java.

For Loop in Java - Java For Loop Examples & Syntax


The for loop is the type of  looping construct. It also works as while loop construct but it provide the initialization, condition and the increment  is same written in the for construct. All the statements which has to be executed written in the for block. We can use the multiple for loop in a program or a for loop also. When we write a loop under another loop then the second loop is called nested loop.

The Syntax for the For Loop:  

  for( initialization; termination; increment)

The given example illustrates that how to use the for loop for developing a application or a program. In this program we will see that the initialization of the variable, condition and increment used for the for loop. Initialization, Condition and Incrementation, all are optional but two semicolons are compulsory which separates Initialization, Condition and Incrementation.

Initialization: It allows the variable to be initialize. Such as: 
int i = 1;
  int j = 1;
Termination (or condition): It allows to check the certain condition. If condition is true then all statements and processes written in the for block will be executed otherwise ignored. Condition such as:
i <= 5;
  j <= i;
Increment: It allows the how much increase the given variable. Such as:

Code of the program : 

public class  ForLoop{
public static void main(String[] args){
  for(int i = 1;i <= 5;i++){
  for(int j = 1;j <= i;j++){

Output of the program : 


Download For Loop Example


Posted on: June 7, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: For Loop in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 23, 2012

how to create a password in java program ?
Rajvir parmar
August 7, 2012
for loop

is this syntax is correct ?? for(int i=0;i++<2) { }
September 23, 2012

i want java loop examples plz help me
October 29, 2012
How to use Row & Column?

How can I used for loop in the Row and Column Java Programming?
Raghuraj Singh
December 4, 2012
for loop

how are initialazation?
July 5, 2013

public class ForLoop{ public static void main(String[] args){ for(int i = 1;i <= 5;i++){ for(int j = 1;j <= i;j++){ System.out.print(i); } System.out.println(); } } }
July 22, 2013
for loop

useful..ilike it
November 8, 2013

dude your second for loop must be for(j=0; j<=i; j++) to output the 1 in the program. :) Thanks btw.
likhna sunil
February 5, 2014

i want java for loop examples