Java for Loop Iteration Syntax
Posted by Rex in Java programming
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 as follows:
for ( {initialization}; {exit condition}; {incrementor} )
code_block;
For example, to execute a simple loop five times, printing out the iteration number each time through the loop, use the following syntax:
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, the exit condition is evaluated (Is i less than 6?); and if true, it 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. Thus, 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.
The Conversation
Follow the reactions below and share your own thoughts.



January 27, 2009 at 2:18 pm, Anonymous said:
i want many type of for loop code
April 29, 2009 at 8:06 am, seema said:
good
June 22, 2009 at 9:12 pm, Peter said:
concerning i
February 28, 2011 at 7:54 am, GameMaster said:
how about for stacking??
June 30, 2009 at 10:03 am, Gobitha said:
please give appropriate generic syntax form to this for loop
“for(int i:arr){System.out.println(i);}”
July 26, 2009 at 2:29 pm, Anonymous said:
simpl program for loop
August 24, 2009 at 5:03 am, Anonymous said:
how to do the program and output like this:
#######
######
#####
######
#####
###
##
#
February 28, 2010 at 9:05 pm, Anonymous said:
for(int a=5;a>=1;a–)
{
System.out.println();
for(int b=1;b
January 26, 2011 at 3:16 pm, Shennie Geronimo said:
nice
June 11, 2012 at 9:01 am, RICLA said:
> here is your answer cool baby:)))))))))))
for(int i=7; i>=5; i–)
{
for(int j=0;j=5; i–)
{
for(int j=0;j=1; i–)
{
for(int j=0;j
October 12, 2009 at 1:47 am, Anonymous said:
please fix it for me!
/**
* @(#)hehe.java
*
*
* @author
* @version 1.00 2009/10/7
*/
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
February 28, 2010 at 9:08 pm, Anonymous said:
please use switch case, if else statements are not very suitable like this program
November 20, 2009 at 2:14 pm, Anonymous said:
the description is not very good
December 31, 2009 at 11:07 am, Anonymous said:
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.
February 11, 2010 at 3:04 pm, Anonymous said:
how to write a java program for following output?
*
* * *
* * * * *
* * *
*
February 28, 2010 at 9:17 pm, Anonymous said:
for(int a=1;a=1;c–)
{
for(int d=1;d
February 25, 2010 at 8:25 am, Anonymous said:
how to program like this using java case
Create Account:
Age
Name
Address
Change Pin
March 09, 2010 at 4:02 am, Anonymous said:
how to do this:
######
# #
# #
# #
# #
######
July 13, 2010 at 2:20 am, Da大哥ge said:
for(int c=1;c
July 26, 2010 at 7:31 am, jerome said:
what is the code for this one:
5 = *****
4 = ****
3 = ***
2 = **
1 = *
—-anybody know?? please help
August 12, 2010 at 5:13 am, bryan said:
public class asteris{
public static void main(String[] args){
int I=0,j=0;
while(I
October 20, 2010 at 12:06 pm, Pkannan said:
How to display
1111
2222
3333
4444
using for loop?
February 19, 2011 at 3:23 am, Miracle_ss said:
class For5
{
public static void main(String args[])
{
int array[][]=new int[4][5];
int k=1;
for(int i=0; i
November 12, 2010 at 1:45 am, francis said:
how about dis one?
*
***
****
*******
*********
November 18, 2010 at 12:17 pm, van said:
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
December 01, 2010 at 3:25 am, Drew T said:
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?
January 18, 2011 at 11:18 am, Musee21 said:
if i type 4 then the result…
****
****
****
****
anyone can teach me how?…..i need that program this January 20, 2011..ASAP…please help me..
February 01, 2011 at 8:35 am, Ajuopel said:
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
January 21, 2011 at 10:17 am, Rea_sweety1990 said:
*
**
***
****
how to do this
January 25, 2011 at 8:00 am, Ajuopel said:
how 2 program in java if i enter 4 the out put is like this
4
****
****
****
****
January 25, 2011 at 9:40 am, Yalmamatter said:
how to program the ff. output?
333
444
555
February 01, 2011 at 4:58 am, Yalmamatter said:
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,,
February 01, 2011 at 5:02 am, best programmer of jhcsc said:
to ajuopel and yalmamatter,,just answer on your own,,,dont depend on me ok??hahhahaha
February 01, 2011 at 8:05 am, Ajuopel said:
boang ka bcen borit rna weeeeeeeeeee
February 08, 2011 at 10:02 am, best programmer of jhcsc said:
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?
February 08, 2011 at 10:07 am, best programmer of jhcsc said:
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?
February 22, 2011 at 8:44 pm, sri said:
how to print as follows?
*
* *
* * *
* * * *
February 24, 2011 at 9:44 pm, Parth Thirteen said:
how do i give the option to exit or to go back to begining, such as a calculator code
February 26, 2011 at 9:43 am, Janani_kaushalya said:
how to write a java program to display following design
#
##
###
####
February 27, 2011 at 12:49 pm, Sampath Liyanage said:
for(int a=1;a
February 26, 2011 at 9:47 am, Janani_kaushalya said:
how to write a java program to display following design
???#
???##
???###
####
May 17, 2011 at 8:01 am, Chris said:
nice coments
June 26, 2011 at 5:18 pm, Amtechxl said:
how to write a java program to get a o/p as follows?
1
12
123
1234
12345
July 05, 2011 at 10:55 am, Bob said:
So, just how many people are looking for homework answers here……?
July 09, 2011 at 11:36 am, Rampravesh Mourya said:
what will be the code of
1
2 2
3 3 3
4 4 4 4
December 04, 2011 at 4:15 am, Nhoriel said:
How to make a program that it’s output is like this?
*
**
***
****
*****
March 12, 2012 at 11:00 pm, Itayulita said:
Bagaimana cara penulisan syntak mempunyai hasil seperti :
number 1 = 12
number 2 = 32
number 3 = 10
nilai yang tertinngi number 2 = 32
August 20, 2012 at 6:00 am, shwetha bhat said:
how to do the program based on the loop for the sum and average.Using (for) condition
January 09, 2013 at 9:24 am, janu said:
easily to understand