Java for loop

The basic for loop structure

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

Allow’s look at some simple examples of the usage of for loop and the commonplace pitfalls in the use of for loop

1. Providing expression in for loop is must :

For loop ought to consist of a legitimate expression in the loop statement failing that can cause a limitless loop. The statement

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

 

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

}

Output: This code prints the statement “This is an infinite loop” repeatedly.

2. Initializing multiple variables :

In java, multiple variables can be initialized in the initialization block of for loop irrespective of whether you use it in the loop or not.

// Java program to illustrate
// Initializing multiple variables
// in initialization block
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 code above, there are some simple changes to the for loop. Two variables are declared and initialized in the initialization block. The variable "z" is not used. In addition, the other two components contain additional variables. Therefore, it can be seen that these blocks may contain additional variables that may not be referenced by each other.

3. Redeclaration of a variable in initialization block : Assume, an initialization variable is already declared as integer. Are we able to re-declare it in for loop with different records kind? No, see the example:

// Java program to illustrate
// redeclaring a variable
// in initialization 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 + " ");
		}
		
	}
}

 

Example3.java:12: error: variable x is already defined in method main(String[])
        for(long y = 0, x = 1; x < 5; x++)

Here, x has been initialized to zero as an integer and re-declared with the long data type in the loop. But this problem can be solved by slightly modifying the code. Here, the variables x and y are declared in a Different methods.

// Java program to illustrate
// redeclaring a variable
// in initialization block
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 + " ");
		}
		
	}
}

Output:

1 2 3 4

4. Variables declared in the initialization block must be of same type : It is just a common sense that when we declare a variable as

 int x, y;

both variables are of same type. Its just the same in for loop initialization block too.

 
// Java program to illustrate
// declaring a variable
// in initialization block
public class Example4
{
	public static void main(String[] args)
	{	
		// This will cause error;
		// int x;
		
		// redeclaring x as long will not work
		for (long y = 0, x = 1; x < 5; x++)
		{
			System.out.print(x + " ");
		}
		
	}
}

5. Variables in the loop are accessible only within: The variables which are declared in the initialization block can be accessed most effective in the loop. For a greater scope of variables, refer here

// Java program to illustrate
// scope of Initializing variables
// within the loop
public class Example5
{
	public static void main(String[] args)
	{
		// x and y scope is only
		// within for loop
		for(int x = 0, y = 0; x < 3 && y < 3; x++, y++)
		{
			System.out.println(y + " ");
		}
	
		System.out.println(x);
	}
}

Error

Example5.java:13: error: cannot find symbol
        System.out.println(x);

 

Submit Your Programming Assignment Details