Java for loop iteration syntax
The for loop provides a simple mechanism for repeating a code block a fixed number of times or for iterating through a set of values.
The generic syntax for a Java for loop is:
for ( {initialization}; {exit condition}; {incrementor} )
code_block;
For example, to execute a simple loop 5 times printing out the iteration number each time through the loop:
for ( i=1; i<6; i++ ) {
System.out.println(i);
}
In this example, the loop starts with an initialization of the (int) variable i to the value 1. Each time the loop iteration starts, a the exit condition is evaluated (is i less than 6?) and if true, continues. When the loop iteration is complete, the incrementor code is executed (i++ means increment the variable i by one). The loop continues until the exit condition is met (when i is equal to or greater than 6), so the code block will be executed for i values of 1, 2, 3, 4, and 5.
The exit condition code can be more complex as needed because it is computed each time through the loop, so the condition can be based on variables which may change during the loop iteration. To run a code block on each value of an array, the loop condition could be i < array.length.
The incrementor code can also be more complex, essentially any valid Java code. Another simple example would be a decrementor using i-- which would subtract one from the variable i each time through the loop.











Anonymous said on January 27, 2009
i want many type of for loop code
seema said on April 29, 2009
good
Peter said on June 22, 2009
concerning i
Gobitha said on June 30, 2009
please give appropriate generic syntax form to this for loop
“for(int i:arr){System.out.println(i);}”
Anonymous said on July 26, 2009
simpl program for loop
Anonymous said on August 24, 2009
how to do the program and output like this:
#######
######
#####
######
#####
###
##
#
Anonymous said on October 12, 2009
please fix it for me!
import java.io.*;
public class hehe{
public static void main(String args[]) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int user;
int pass;
int i=1;
String yesno;
char yorn;
while(i
Anonymous said on November 20, 2009
the description is not very good
Anonymous said on December 31, 2009
This page was created in 2004? Is this site ever used or updated? This page could stand to be updated to account for the enhanced for loop which provides a much easier way to traverse an array.
Anonymous said on February 11, 2010
how to write a java program for following output?
*
* * *
* * * * *
* * *
*
Anonymous said on February 25, 2010
how to program like this using java case
Create Account:
Age
Name
Address
Change Pin
Anonymous said on February 28, 2010
for(int a=5;a>=1;a–)
{
System.out.println();
for(int b=1;b
Anonymous said on February 28, 2010
please use switch case, if else statements are not very suitable like this program
Anonymous said on February 28, 2010
for(int a=1;a=1;c–)
{
for(int d=1;d
Anonymous said on March 9, 2010
how to do this:
######
# #
# #
# #
# #
######
Da大哥ge said on July 13, 2010
for(int c=1;c
jerome said on July 26, 2010
what is the code for this one:
5 = *****
4 = ****
3 = ***
2 = **
1 = *
—-anybody know?? please help
bryan said on August 12, 2010
public class asteris{
public static void main(String[] args){
int I=0,j=0;
while(I
Pkannan said on October 20, 2010
How to display
1111
2222
3333
4444
using for loop?
francis said on November 12, 2010
how about dis one?
*
***
****
*******
*********
van said on November 18, 2010
How do I creat a for loop which accepts an integer value/values for the variable age, and displays number from up to the entered number.
Example:
Please enter age:3
lets Count!
1
2
3
Drew T said on December 1, 2010
how do u make a code tht takes a number from the user and adds up by it like if the person was to put 2 it would go 2 4 6 8 all the way up to 100?
Musee21 said on January 18, 2011
if i type 4 then the result…
****
****
****
****
anyone can teach me how?…..i need that program this January 20, 2011..ASAP…please help me..
Rea_sweety1990 said on January 21, 2011
*
**
***
****
how to do this
Ajuopel said on January 25, 2011
how 2 program in java if i enter 4 the out put is like this
4
****
****
****
****
Yalmamatter said on January 25, 2011
how to program the ff. output?
333
444
555
Shennie Geronimo said on January 26, 2011
nice
Yalmamatter said on February 1, 2011
how to program using for loop,if u input 3 the result is :
333
444
555 if input 2 the result is :
22
33 if u input 4 the output is:
4444
5555
6666,,
best programmer of jhcsc said on February 1, 2011
to ajuopel and yalmamatter,,just answer on your own,,,dont depend on me ok??hahhahaha
Ajuopel said on February 1, 2011
boang ka bcen borit rna weeeeeeeeeee
Ajuopel said on February 1, 2011
public static void main(String[]args)
{
int a,b,c;
System.out.println(” press enter number:”);
a=EasyIn.getInt();
b=a;
c=b;
for( a=2; a
best programmer of jhcsc said on February 8, 2011
use english language everytime you post your reply here,use this language so that the world can understand you!!!,,i didnt redicule you when i make my reply iim just stating the fact,please learn to grow up and solve your own program?
best programmer of jhcsc said on February 8, 2011
use english language everytime you post your reply here,use this language so that the world can understand you!!!,,i didnt redicule you when i make my reply iim just stating the fact,please learn to grow up and solve your own program?
Miracle_ss said on February 19, 2011
class For5
{
public static void main(String args[])
{
int array[][]=new int[4][5];
int k=1;
for(int i=0; i
sri said on February 22, 2011
how to print as follows?
*
* *
* * *
* * * *
Parth Thirteen said on February 24, 2011
how do i give the option to exit or to go back to begining, such as a calculator code
Janani_kaushalya said on February 26, 2011
how to write a java program to display following design
#
##
###
####
Janani_kaushalya said on February 26, 2011
how to write a java program to display following design
???#
???##
???###
####
Sampath Liyanage said on February 27, 2011
for(int a=1;a
GameMaster said on February 28, 2011
how about for stacking??
Chris said on May 17, 2011
nice coments
Amtechxl said on June 26, 2011
how to write a java program to get a o/p as follows?
1
12
123
1234
12345
Bob said on July 5, 2011
So, just how many people are looking for homework answers here……?
Rampravesh Mourya said on July 9, 2011
what will be the code of
1
2 2
3 3 3
4 4 4 4
Nhoriel said on December 4, 2011
How to make a program that it’s output is like this?
*
**
***
****
*****