A loops statement allows us to execute a statement or group of statements
multiple times and following is the general form of a loop statement in most
of the programming languages . To gain a bit more control over the execution of a loop, we have another option. The for loop requires three different statements to operate. The first statement is called an initialization, the second is a condition, and the third is an operation.

Syntax:

for(initialization;
Boolean_expression; update)
{
// Statements
}

Here is the flow of control in a for loop

Flow Chart:

For Loops

Example:
Following is an example code of the for loop in Java. In this example we displays the values between 10 to 20 using the for loop. Here int x=10 is the initialization x < 20 is the loop condition i.e. loop will be continue if the value is greater than 10 and less than 20, and x=x+1 is a increment. The print statements in the programs are the conditional code which is execute when the loop condition is true.

public class Test {
public static void main(String args[])
{
for(int x = 10; x < 20; x = x + 1) 
{
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}

This will produce the following result:
Output:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

To know about what is loop in java and loops control statements see our article named Loops and Loops Control Statements in Java.

Leave a Reply

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

Back to Top