Java For Loop – A Quick Guide with Java For Loop Example

1. Objective

In this Java tutorial, we are going to study about Java For loop and examples of Java for loop and many other things. As we have already studied the basics of decision statements in Java.

So, let’s start with Java for loop in detail.

Java For Loop - A Quick Guide with Java For Loop Example

Java For Loop – A Quick Guide with Java For Loop Example

2. What is Java For Loop?

Java For loop syntax –

for(initialization; boolean expression; update statement)
{
    //Body
}

Read about Loops in Java in Detail

3. Java For Loop Example

Here, we discuss some Java For loop example with some pitfalls. In addition, we discuss these examples with syntax and example programs.

a. Providing expression in for loop is a must

Syntax –

for ( ; ; )
is similar to
while(true)

Example –

public class Example1
{
    public static void main(String[] args)
    {
        for( ; ; )
        {
            System.out.println("This is an infinite loop");
        }
    }
}

Output –

This is an infinite loop

Let’s Discuss java.io.File Class in Java

b. Initializing multiple variables

public class Example2
{
    public static void main(String[] args)
    {
        int x = 2;
        for(long y = 0, z = 4; x < 10 && y < 10; x++, y++)
        {
            System.out.println(y + " ");
        }
            System.out.println(x);
    }
}

In the above code, there is basic variety in the for a circle. Two factors announce and introduce in the in statement square. The variable ‘z’ isn’t being utilized. Likewise, the other two parts contain an additional variable. In this way, it can see that the squares may incorporate additional factors which may not be referenced by each other.

c. Redeclaration of a variable in an initialiation block

public class Example3
{
    public static void main(String[] args)
    {   
        // x is integer
        int x = 0;
        // redeclaring x as long will not work
        for(long y = 0, x = 1; x < 5; x++)
        {
            System.out.print(x + " ");
        }
    }
}

Read about How Java Inheritance is used in detail
Here, x and y declare in a different way-

public class Example3
{
    public static void main(String[] args)
    {   
        // x is integer
        int x = 0;
        long y = 10;
        for (y = 0, x = 1; x < 5; x++)
        {
            System.out.print(x + " ");
        }     
    }
}

It was already initialized to zero as integer and is being re-declared in the Java For loop with data type long.

d. Variables declared in the initialization block must be of the same type

public class Example4
{
    public static voi main(String[] args)
    {   
        for (long y = 0, x = 1; x < 5; x++)
        {
            System.out.print(x + " ");
        }
   }

It must have common initialization block too.

e. Variables in the loop are accessible only within

public class Example5
{
    public static void main(String[] args)
    {
        for(int x = 0, y = 0; x < 3 && y < 3; x++, y++)
        {
            System.out.println(y + " ");
        }   
        System.out.println(x);
    }
}

In the above code, there is a basic variety in the form of a circle. Two factors announce and introduce in the in statement square. The variable ‘z’ isn’t being utilized. Likewise, the other two parts contain an additional variable. In this way, it can see that the squares may incorporate additional factors which may not reference each other.
Let’s Discuss Access Modifiers in Java 

This was all about Java For Loop Tutorial. Hope you like our explanation.
Java Quiz

4. Conclusion

In this tutorial, we learned about the Java for loop, how it uses and various pitfalls with Java For loop example relate to it. Furthermore, if you have any query feel free to ask in a comment section.

Related Topic- Java Array Tutorial 

For reference 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.