Javascript required
Skip to content Skip to sidebar Skip to footer

How to Have a for Loop Run Again With a Different Variable

Looping in programming languages is a characteristic that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to truthful. In Java, only unlikely any programming language provides four ways for executing the loops namely while loop, for loop, for-each loop, do-while loop or we tin can call information technology basically three types of loops in some books equally for-each loop is treated as enhanced for loop. Let united states hash out for loop in details.

Generally, we tend to use while loops equally nosotros do get a better agreement if we are into learning loops but after a saturation, nosotros equally programmers tend to tilt towards for loop as it is cleaner and foundations are carried out in a directly become for which nosotros accept to carefully grasp syntax as follows:

Syntax: Information technology consists of three parts namely as listed:

  • Initialization of variables
  • Specific condition as per requirement over which these defined variables are needed to be iterated
  • A terminating part where nosotros generally play with variables to reach to terminating condition state.
for(initialization; boolean expression; update argument) {  // Body of for loop  }        

This is generally the basic pilgrimage structure of for loop.

Let's await at some basic examples of using for loop and the common pitfalls in using for loop which enables u.s.a. to know even better equally internal working will be depicted as we volition be playing with codes.

Usecase one: Providing expression in for loop is a must

For loop must consist of a valid expression in the loop statement failing which tin can pb to an infinite loop. The statement

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

Note: This to a higher place said is crux of advanced programming equally it is origin of logic building in programming.

Instance

Java

public class GFG {

public static void primary(Cord[] args)

{

for (;;) {

System.out.println( "This is an space loop" );

}

}

}

Output: Prints the argument "This is an infinite loop" repeatedly.

This is an space loop This is an infinite loop This is an infinite loop This is an space loop ... ... This is an infinite loop

Usecase 2: Initializing Multiple Variables

In Java, multiple variables can be initialized in the initialization block of for loop regardless of whether y'all use it in the loop or non.

Example:

Java

public form GFG {

public static void principal(Cord[] args)

{

int ten = 2 ;

for ( long y = 0 , z = four ; x < ten && y < ten ;

x++, y++) {

Organisation.out.println(y + " " );

}

Organization.out.println(x);

}

}

In the above code, there is simple variation in the for loop. Ii variables are alleged and initialized in the initialization cake. The variable 'z' is not being used. Also, the other ii components contain actress variables. So, it can be seen that the blocks may include extra variables which may non be referenced past each other.

Usecase 3: Redeclaration of a Variable in the Initialization Cake

Suppose, an initialization variable is already declared as an integer. Hither we can non re-declare it in for loop with another information type as follows:

Example one:

Java

public form GFG {

public static void main(Cord[] args)

{

int x = 0 ;

for ( long y = 0 , x = one ; ten < five ; x++) {

System.out.print(10 + " " );

}

}

}

Output:

Example3.java:12: fault: variable x is already defined in method chief(Cord[])         for(long y = 0, 10 = 1; x < 5; x++)

Hither, ten was already initialized to zippo equally an integer and is existence re-declared in the loop with data type long. Merely this problem can be fixed by slightly modifying the lawmaking. Here, the variables ten and y are declared in a unlike style.

Example 2:

Java

public class GFG {

public static void primary(String[] args)

{

int x = 0 ;

long y = ten ;

for (y = 0 , x = 1 ; x < 5 ; x++) {

System.out.print(x + " " );

}

}

}

Output:

1 2 3 four

Usecase four: Variables declared in the initialization block must be of the same type

It is just common sense that when we declare a variable equally shown below:

          int x, y;

Here both variables are of the same type. It is just the aforementioned in for loop initialization cake too.

Example:

Java

public class GFG {

public static void main(String[] args)

{

for ( long y = 0 , x = ane ; x < 5 ; 10++) {

Organisation.out.print(10 + " " );

}

}

}

Usecase 5: Variables in the loop are attainable but within

The variables that are declared in the initialization block can be accessed merely within the loop as we have as per the concept of the telescopic of variables.

Example:

Java

public class GFG {

public static void master(Cord[] args)

{

for ( int x = 0 , y = 0 ; x < 3 && y < iii ; 10++, y++) {

System.out.println(y + " " );

}

Organisation.out.println(ten);

}

}

Error:

Example5.java:13: fault: cannot find symbol         System.out.println(10);

In the above instance, variable x is not accessible outside the loop. The statement which is commented gives a compiler mistake.

This article is contributed past Preeti Pardeshi. If you like GeeksforGeeks and would like to contribute, you tin also write an article using write.geeksforgeeks.org or postal service your commodity to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks principal folio and assist other Geeks. Please write comments if you discover annihilation incorrect, or you desire to share more data about the topic discussed above.


gregoryvist1985.blogspot.com

Source: https://www.geeksforgeeks.org/for-loop-java-important-points/