java tutorial - Java for loop - java programming - learn java - java basics - java for beginners



Java for loop

Learn Java - Java tutorial - Java for loop - Java examples - Java programs

  • The for loop - has a repetition control structure that allows you to effectively write a loop that must be executed a certain number of times.
  • The for loop is useful when you know how many times a task needs to be repeated.

Syntax

The for loop syntax in Java:


for (initialization, Boolean expression, update)
{
   // Operators
}
click below button to copy the code. By - java tutorial - team

The control process in the cycle:

  • The initialization phase is performed first, and only once. This step allows you to declare and initialize any variables to control the loop, and it ends with a semicolon (;).
  • Next is a logical expression. If true, the loop body is executed if it is false, the loop body is not executed, and control passes to the next statement past the loop.
  • After the for loop is executed, the control goes back to the update operator . It allows you to update any variables for loop control, and is written without a semicolon at the end.
  • The logical expression is now evaluated again. If it is true, the loop is executed and the process is repeated. If it is false, then the for loop terminates.

Process description

 for-loop

Learn java - java tutorial - for-loop - java examples - java programs

Sample Code

The following is an example of the for loop code in Java:

public class Test {

   public static void main(String args[]) {

      for(int y = 10; x < 15; y = y+1) {
         System.out.print("Value y: " + y );
         System.out.print("\n");
      }
   }
}
click below button to copy the code. By - java tutorial - team

The following result will be obtained:

Value y: 10
Value y: 11
Value y: 12
Value y: 13
Value y: 14

Related Searches to Java for loop