Java For Loop

 

For loop executes one or more statements for a specified number of times. This loop is also counter controlled loop. It is the most flexible loop. That is why the most programmers use this loop in programs. In Java there are three types of loop:

  1. Simple For loop
  2. For each and enhanced For loop
  3. Labeled For loop

 

Java Simple For loop:

The simple for loop in Java is same as the “for loop” in C or C++ programming languages. In the simple for loop we initialize a variable and then the for loop checks a condition and an increment or decrement is performed at the variable, if the condition is true then the control enters the body of the loop and after executing the statements the control is again transferred to the loop. The variable in the “for loop” is also called counter variable and the “for loop” is also called counter controlled loop.

Syntax:

The following is the syntax of simple for loop in Java:

For (initialization; condition; increment/decrement)

{

Statements

}

Initialization:

It specifies the starting value of loop variable or counter variable.

Condition:

The condition is given as a relational expression. The statements in the body of the loop are executed only if the condition is true.

Increment/decrement:

This part specifies the change in counter variable. In this part either the value of the counter variable is incremented by a specified number or the value of the counter variable is decremented by a specified number.

Statement:

Statement is the instruction or the code of Java that is executed when the condition is true.

 

Working of “for” loop:

The number of iteration depends on the initialization, condition and increment/decrement parts. The initialization part is only once executed when the control enters the loop. After initialization, the given condition is evaluated. If it is true, the control enters the body of the loop and executes all statements in it. Then the increment/decrement part is executed that changes the value of counter variable either increase or decrease.

The control again moves to condition part. This process continues while the condition remains true. The loop is terminated when the condition becomes false.

 

Flowchart of simple for loop in Java:

flowchart-of-simple-for-loop-in-java

Example of simple for loop in Java:

Consider the following example in which the simple for loop in java is used to print a string a number of times.

CODE:

public class ExampleFor {

public static void main (string [] args) {

for (int n=1;n<=5;n++)

{

system. out. println (“Country”);

}

}

}

OUTPUT:

Country

Country

Country

Country

Country

In the above example the counter variable is declared inside the “for loop” and is initialized at 1. Now the value of the variable is 1, the condition is checked as 1 is smaller than 5 the condition is true the control will enter the body of the loop and execute the statement. In the body of the loop there is a print statement that prints “Country”, “Country” is printed.

Then the counter variable will be increment by 1. Now the condition is checked, the value of counter variable is 2 this time as it is smaller than 5 the control will again enter the body of the loop and “Country” is printed. This will happen until the condition becomes false. That is when the value of the counter variable becomes 6 the loop will break and the string is no more printed. In this way we have printed our string five times.

 

Java For each loop:

In Java we use the “for each” loop to iterate through an array. The difference between the simple for loop and the “for each” loop is that we do not need any increment in the “for each” loop. Instead subscript notation is used. The “for each” loop can be applied to elements only and not the index of the arrays.

 Syntax:

The following is the syntax of the “for each” loop in Java:

for (Type var: array) {

statements

}

 

Example of “for each” loop in Java:

Consider the following example in which we have used the “for each” loop to iterate through an array in Java:

CODE:

public class ExamplForEach {

public static void main (string [] args) {

int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

for (int j: a) {

system. out. println (j);

}

}

}

OUTPUT:

1

2

3

4

5

6

7

8

9

In the above example an array has been declared that has 9 elements in it. Then we used the “for each” loop. The loop has a counter variable j which iterates through the array. The loop will be executed till the end of the array is reached. Each element of the array is assigned to the counter variable and the counter variable is printed. It can be seen that there is no increment or decrement in the counter variable and there is no condition also.

 

Java labeled for loop:

In Java programming language we use labels for the “for loops” so that it become easier for the user that which loop should be broken using the break keyword or which loop should be continued using the continue keyword. This is needed when we are using the nested loops. We name the outer loop and the inner loop using a label and through that label we can continue or break the loop using continue and break statements.

In general the break and continue statements break and continue only the inner loops but by labeling we can break and continue the outer loops also.

Syntax:

The following is the syntax of the labeled for loop in Java programming language:

label name:

for (initialization; condition; increment/decrement) {

statements;

}

Example of labeled for loop in Java:

Consider the following example in which we have used nested loop and to break the outer and inner loops we used a label:

CODE:

public class ExampleLabeledFor {

public static void main (string [] args){

outer:

for (int j = 1; j <= 3; j++) {

inner:

for (int i = 1; i <= 3; i++) {

if (i < =3) {

break outer;

}

system. out. println (i + “ ” +j);

}

}

}

}

OUTPUT:

1 1

2 1

In the above example we used nested loop and the outer loop is labeled as outer and the inner loop is labeled as inner. We broke the outer loop by using its label.

 

Java infinitive for loop:

In Java a simple for loop can be made infinitive by using two semicolons in the loop. The syntax of the infinitive for loop in Java is as follows:

SYNTAX:

for (;;) {

statements

}

Example of infinitive for loop in Java:

Consider the following example in which we have used the infinitive for loop to print a string:

CODE:

public class ExampleFor {

public static void main (string [] args) {

for (;;) {

system. out. println (“Infinitive for loop in Java”);

}

}

}

OUTPUT:

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

Infinitive for loop in Java

The loop will not break until the user press ctrl + c. The combination that is ctrl + c is used to exit from the loop or program.