All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Advanced For Loop In Java

Last Updated : Mar 11, 2024

Advanced For Loop In Java

In this article we will show you the solution of advanced for loop in java, unlike usual for loop in java alternative method is launched known as for-each() loop.

Its preferably most readable than normal for loop and simple syntax. Also, you cannot do some of thing like for() loop to know about it in detail with example program refer below.

Step By Step Guide On Advanced For Loop In Java :-

In main class ‘Simple_prgm’ we given main method definition. There we provide sample for for-each loop use and it syntax is:

for(data type variable name: array variable or collection)

Within main method first we defined array variable with some integer values. With the help of it we showing usage of advanced for loop.

To give instruction regards program we printed program title in println() method. Then for-each loop defined with array variable and printed array values one by one as output at the terminal.

//For-each loop
package java_prgms;
class Simple_prgm
 public static void main(String args[])
  int a[]={9,3,12,56};
  System.out.println("Using Advanced For Loop Printed Array Values");
  for(int t:a)
  1. The ‘Package java_prgms’ may differ as per developer definition and it is container name where your overall data store. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. We defined main class with name ‘Simple_prgm’ then we given public static void main method definition. There we created array 'a' with 4 onteger values '9,3,12,56'. If you want to change or use any other array values you can modify it accordingly.
  3. Using println() method we displaying text 'Using Advanced For Loop Printed Array Values' as first line of output.
  4. As per syntax we defined for loop, here variable 'i' pointing array values directly without position at each iteration. At the right hand side you have to give wanted array or collection variable, so we given 'a'.
  5. Then we printing each array values one by one. If you defined array with some other value then you will get output as per modification.
  6. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know about advanced for loop and its proper use. To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Simple_prgm‘ line for gets the output.

Then you can see 'Using Advanced For Loop Printed Array Values' text and values '9,3,12,56' one by one order.

On eclipse just press run button then you will get output inside terminal.

I hope this article on advanced for loop in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪