Understanding For Loop in Java With Examples and Syntax

[ad_1]

Loops in Java is a feature used to execute a particular part of the program repeatedly if a given condition evaluates to be true.

While all three types’ basic functionality remains the same, there’s a vast difference in the syntax and how they operate.

In this article, you will focus on for loop in Java. But before delving deep into for loop and how to use it, let’s understand the difference between the three types of loops.

Types of For Loops in Java

There are three types of for loops in Java:

  • Simple
  • For-each or enhanced
  • Labeled

You will go through each type of Java for loops with examples.

Simple For Loop in Java

A simple for loop is what you have seen until now, including the flow and syntax. Here’s an example where you must print the values from 1 to 10.

Example of simple for loop:

public class forExample

public static void main(String args[])

for(int x=1; x<=10; x++)

             System.out.println(x);   

        

    

Output:

TypeForLoop

For-each Loop in Java

The Java for-each loop is used on an array or a collection type. It works as an iterator and helps traverse through an array or collection elements, and returns them. While declaring a for-each loop, you don’t have to provide the increment or decrement statement as the loop will, by default, traverse through each element. Here’s the syntax:

for(Type var:array)  

//loop body

Example of for-each loop:

public class ForEach  

    public static void main(String[] args)  

        //Array declaration  

        int ar[]=1,2,3,5,7,11,13,17,19;  

        //Using for-each loop to print the array  

        for(int x:ar)

            System.out.println(x);  

          

      

Output:

ForEachLoop

Labeled For Loop in Java

With the labeled for loop in Java, you can label the loops. It is useful when you have a nested loop (more about it later) and want to use the break or continue keyword for the outer loop instead of the inner one. Usually, the break and continue keyword works on the innermost loop by default. The syntax of Java labeled for loop is:

labelname:  

for(initialization;condition;incr/decr)  

//loop body

Example of labeled for loop

public class LabeledForLoop{  

    public static void main(String[] args)  

        //Using Labels 

        Label1:  

            for(int x=1;x<=5;x++)  

                Label2:  

                    for(int y=1;y<=4;y++)  

                        if(x==3&&y==2)  

                            break Label1;  

                          

                        System.out.println(x+” “+y);  

                      

              

      

}

Output:

LabeledForLoop

As you can see in the above example, this demo has used the label name to break the outer loop, which is the opposite of a loop’s default behavior.

Nested For Loop in Java

Java nested for loop is not a separate type of loop. It is just using one or multiple for loops inside another. Whenever the outer loop meets the condition, the inner loop is executed completely. It is usually used for pattern programs to print distinct patterns in the output. The example below uses the nested for loop in Java to print a pyramid.

public class NestedForExample  

    public static void main(String[] args)  

        for(int x=1;x<=7;x++)

            for(int y=1;y<=x;y++)  

                System.out.print(“* “);  

              

            //new line when the inner loop is executed completely

            System.out.println();

          

      

Output:

NestedForLoop

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now

FREE Java Certification Training

Infinite For Loop in Java

The Java infinite for loop is used if you want to keep running a certain set of code. Syntax of infinite for loop in Java is:

for(;;)

//loop body

Example of infinite for loop

public class InfiniteFor  

    public static void main(String[] args)   

        //Declaring the infinite for loop  

        for(;;)  

            System.out.println(“Simplilearn”);  

          

      

Output:

infiniteForLoop.

You can use ctrl + c to exit the infinite loop.

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

[ad_2]

Source link