Extended Version of for Loop in Java

As of Java 5, the upgraded for loop was presented. This is basically utilized for Arrays. The syntax for this loop is as follows:

for(declaration : statement)
{
//Statements
}

  • Declaration: The recently declared variable, which is of a sort perfect with the components of the show you are getting to. The variable will be accessible inside the for piece and its esteem would be the same as the current array component.
  • Expression: This assesses to the exhibit you have to loop through. The interpretation can be an array variable or function call that returns an array.

Sample Implementation:

public class myTest {

public static void main(string args[]){int [] mynumber = {0, 5, 10, 15, 20};

for(int i : mynumber ){

System.out.print( i );

System.out.print(” ,”);

}

}

This would deliver the accompanying result:

0, 5, 10, 15, 20


 

For any query or issue, feel free to discuss on http://discuss.eduguru.in