Java Tutorial

Basic Concepts

Flow Control Statements

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>For Loop Control Statements Example in Java

Java Tutorial

Basic Concepts

Flow Control Statements

Object Oriented Concepts

Coming Tutorials



Java For, For Each Loops Syntaxes and Examples

This is the most commonly used loop in Java. The Java for loop allows conveniently iterate block of code for a specific number of repetitions. The Java for loop is mostly used for index based data structure manipulations, like inserting and retrieving values in Lists, Maps, Arrays, etc.


Syntax:

for (initialization; condition; increment or decrement){

statements;

....;

....;

}

Note: Curly braces are optional and without curly braces we can take only one statement which should not be declarative statement.


1. Initialization Section:

The initialization section statements will be executed only once.

Usually we are declaring and performing initialization for the variables in this section.

In for loop initialization we can declare multiple variables of the same type but different data type variables are not allowed.

Example:


for(int i=0,j=4;;){}  //valid
for(int i=0,byte j=4;;){}  //invalid
for(int i=0,int j=4;;){}  //invalid

In the initialization section we can take any valid java statement including System.out.println() also.

Example:


int j=0;
for(System.out.println("for init"); j<3; j++)
{
System.out.println("J= "+j);
}

2. Conditional Expression:

Here we can take any java expression but the result should be boolean type.

It is optional and if we did not specify then compiler will always place true.


3. Increment and Decrement Section:

We can take any valid java statement including System.out.println(); also.

Example:


int j=0;
for(System.out.println("for init"); j<3;System.out.println("for incr/decr"))
{
System.out.println("J= "+j);
}

Note: All 3 parts of for loop are independent of each other. All three parts of for loop are optional. semicolon (;) is a valid java statement.

for(;;); // valid, represents infinite loop.


How does Java For Loop Execute?



Step 1: Create local variable i and assign 0 to it.

Step 2: Check the condition.

Step 3: If condition is true then execute statements inside for body.

Step 4: Increment the value of i and check the condition.

Step 5: If condition becomes false then control goes to the statement after for loop.

Note: Steps 1, 2, 5 happens only once. The steps 3 and 4 repeats until condition become false.


Example 1: Print the elements of single dimentional array using general for loop


class Test
{
 // main() method
public static void main(String[] args)
{
int[] arr={10,20,30,40,50,60}; //1 d array
for(int i=0; i<arr.length; i++)
{  
  System.out.println("Element = "+arr[i]);
}

}
}

Example 2: Print the elements of 2 d array using general for loop


class Test
{
 // main() method
public static void main(String[] args)
{
int[][] arr={{10,20,30},{40,50,60}};  
for(int i=0; i<arr.length; i++)
{
  for(int j=0; j<arr[i].length; j++)
  {
  System.out.println("Element = "+arr[i][j]);
  }
}

}
}

For Each Loop(Java enhanced for loop)

Java for each loop is the most convenient loop to retrieve the elements of arrays and collections.


Rules of for each loop in Java

  • It is not a general purpose loop.
  • It is useful only to retrieve the elements.
  • It is applicable only for arrays and collections.
  • By using for each loop we should retrieve all values of arrays and collections and it cannot be used to retrieve a particular set of values.

Syntax:

for(datatype variable : array/collections)

{ statements ... }


Example 1: Print elements of single dimensional array by using enhanced java for loop.


class Test
{
 // main() method
public static void main(String[] args)
{
int[] arr={10,20,30,40,50,60,70};
for(int ele :  arr)
{
System.out.println("Element = "+ele);
}
}
}

Output:


Element = 10
Element = 20
Element = 30
Element = 40
Element = 50
Element = 60
Element = 70

Example 2: Print the elements of 2 d array using enhanced java for each loop.


class Test
{
 // main() method
public static void main(String[] args)
{
int[][] arr={{10,20,30},{40,50,60}}; 
for(int[] aa :  arr)
{
  for(int ele :  aa)
  {
  System.out.println("Element = "+ele);
  }
}
}
}

Share the article to help your friends