java tutorial - Java Improved for loop - java programming - learn java- java basics - java for beginners



Java improved for loop

Learn Java - Java tutorial - Java improved for loop - Java examples - Javaprograms

Improved loop for is mainly used to bypass the collection of elements, including arrays. Has been introduced since Java 5.

Syntax

The syntax of the improved for loop:


for (declaration: expression)
{
   // Operators
}
click below button to copy the code. By - java tutorial - team
  • Announcement : Access to the newly declared variable block is made, which is of type compatible with the elements of the array.
  • Expression : calculates the one you need in the array of the loop. The expression can be a variable or an array method, whose call is returned by the array.

Sample Code

public class Test {

   public static void main(String args[]){
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={"ran", "vicky", "surren", "shunk"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}
click below button to copy the code. By - java tutorial - team

Output

10,20,30,40,50,
ran, vicky, surren, shunk,

Related Searches to Java Improved for loop