Home Java Beginners For Loop in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

For Loop in Java

Advertisement
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)
  {
  statements;
  }

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:
 
i++;
  j++; 

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++){
  System.out.print(i);
  }
  System.out.println();
  }
  }
}

Output of the program : 

1
22
333
4444
55555

Download For Loop Example

Advertisement

Liked it!  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.

Posted on: June 7, 2007

Ask Questions?    Discuss: For Loop in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
lin
April 6, 2011
voting in java

voting system, how to increase a vote in the name of the candidate?
k.eswaran
June 8, 2011
java

loop how to use in java
Ashish Pandey
June 23, 2011
Java

1 2 3 4 5 6 7 8 9 10 I want to program using for loop.
Victor
June 24, 2011
Syntax

What are the syntax of 'for loop', 'else if clause', 'do loop', 'select case' & 'sub procedure'
shela
September 9, 2011
sheil

more examples in for loop statements................ that some dificulty....that makes us challenge
Essie
September 22, 2011
ZVJJnNenZmDxfYuR

Well put, sir, well put. I'll crteainly make note of that.
ravi
September 27, 2011
education

verry nice site
Annonymous
October 21, 2011
Java programs

Comprehensive......
Chuck
November 16, 2011
For loop not executed

I am starting to learn Java. In a very simple program I have using eclipse, I have a for loop to print the value of i used in the for statement, ie. For (i=0; i>10; i++) { System.out.println(i);}. I put two statements to print "Begin Now" and "End Now" before and after the For statement. These messages print out but not the value of i. It seems the compiler is skipping the For loop. Any suggestions?
khairul
December 7, 2011
for loop

By using a for loop, write a program that displays the following output (note that 1 inch is 2.54 centimeters): how to create java code to get the output like that? please help me. output; inches centimeters 1 2.5 3 7.6 5 12.7 7 17.8 : : : : 195 495.3 97 500.4 199 505.5
hetaxi
December 20, 2011
to get how to code for the below progra

example to find the volume of multiple boxes with loop and consructor
Arjun
December 23, 2011
for loop in java

Is there is any other syntex apart from general sxntex mentioned above and is the below mention syntex is valid syntex if not please describe it 1.for (int i=0, j=1; i<5; i++; j++) { } 2. for (int i=0, j=1; i<5,j<5; i++, j++) { } 3. for (int i=0, float j=1.0; ; i++, j++) { }
pranav
December 28, 2011
help

give me code for 2 programs. 1. 123 234 345 2. 123 345 567
Libin
January 14, 2013
hi

can u explain how this for loop is working for(int i = 1;i <= 5;i++){ for(int j = 1;j <= i;j++){ System.out.print(i); } System.out.println(); } } }
kazal ghosh
January 14, 2012
java

I like this site for java programming
Sasotona Wennie
January 20, 2012
Thanks!

Thanks, this program is very useful. Specially for Assignment.
Manoj
January 26, 2012
request

please give me the process for tha looping..... how it works?
aarthi
February 15, 2012
help me plz

output: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 hi , i am having difficulty in writing a program with output
izzy
February 16, 2012
good!

good job! i really learn few. :P
david masika
February 22, 2012
java codes

always post codes to my email
fg
February 24, 2012
java

how for loop excute?
charles okeyo
March 10, 2012
other examples of java for loop

i want it
Sunil
March 28, 2012
Learning

Its so simple example
meenu
May 2, 2012
java programing

java is my subject
pravin kumar singh
July 2, 2012
the following given program to display

1. write a for loop, which will produce the following output:- 1 222 33333 4444444 555555555
bashir
July 2, 2012
programing

this is vary good
C.Thirumalai Rajan
July 15, 2012
Nice

very nice ur example......give more examples to my mail... Thank you......
jennifer enaje
July 23, 2012
java programs

how to create a program using java programming language?
yaswanth
July 23, 2012
java

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) { }
Tauhid
September 23, 2012
Java

i want java loop examples plz help me
franco
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?
alfie
July 5, 2013
java

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(); } } }
kenneth
July 22, 2013
for loop

useful..ilike it
Yow
November 8, 2013
idunno

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
java

i want java for loop examples
DMCA.com