for Loop

A for circle is a reiteration control structure that permits you to effectively compose a loop that needs to execute a particular number of times. Afor looping construct is helpful when you know how often an errand is to be rehashed. The syntax for the looping construct is as follows:

The punctuation of a for circle is: 

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

Here is the stream of control in a for circle:

  • The introduction step is executed in the first place, and just once. This step permits you to pronounce and introduce any loop control variables. You are not needed to put an announcement here, the length of a semicolon shows up.

  • Next, the Boolean outflow is assessed. In the event that it is genuine, the assemblage of the loop is executed. In the event that it is false, the assortment of the loop does not execute and stream of control hops to the following articulation past the for circle.

  • After the group of the for circle executes, the stream of control bounced down to the overhaul explanation. This announcement permits you to overhaul any circle control variables. This announcement can be left clear, the length of a semicolon shows up after the Boolean declaration.

  • The Boolean outflow is currently assessed once more. On the off chance that it is genuine, the loop executes and the scope rehashes itself. After the Boolean declaration is false, the for loop ends.

Sample Implementation

public class myTest {
  public static void main(string args[]) {
    for (int i = 0; i < 5; i = i + 1) {
      System.out.print(“i = ”+i);
      System.out.print(“\n”);
    }
  }
}

This would deliver the accompanying result: 

i = 0
i = 1
i = 2
i = 3
i = 4


Extended Version of for Loop in Java

As of Java 5, the upgraded for loop was presented. This is basically utilized for Arrays. The syntax for this loop is as follows:

for(declaration : statement) {
//Statements
}
  • Declaration: The recently declared variable, which is of a sort perfect with the components of the show you are getting to. The variable will be accessible inside the for piece and its esteem would be the same as the current array component.

  • Expression: This assesses to the exhibit you have to loop through. The interpretation can be an array variable or function call that returns an array.

Sample Implementation:

public class myTest {
  public static void main(string args[]) {
    int[] mynumber = {
      0,
      5,
      10,
      15,
      20
    };
    for (int i: mynumber) {
      System.out.print(i);
      System.out.print(”, ”);
    }
  }
}

This would deliver the accompanying result: 

0, 5, 10, 15, 20


The break Keyword

The break keyword is utilized to stop the whole loop execution. The break word must be utilized inside any loop or a switch construct. The break keyword will stop the execution of the deepest circle and begin executing the following line of code after the ending curly bracket. The syntax for using this keyword is as follows:

break;

Sample Implementation:

public class myTest {
  public static void main(string args[]) {
    int[] mynumbers = {
      0,
      5,
      10,
      15,
      20
    };
    for (int i: mynumbers) {
      if (i == 15) {
        break;
      }
      System.out.print(i);
      System.out.print(“\n”);
    }
  }
}

This would deliver the accompanying result:

0
5
10


The Continue Keyword

The proceed with decisive word can be utilized as a part of any of the loop control structures. It causes the loop to quickly bounce to the following emphasis of the loop.

  • In a for circle, the continue keyword reasons stream of control to quickly bounce to the overhaul articulation.

  • In a while or do/while loop, stream of control instantly hops to the Boolean interpretation.

The syntax of using this keyword is as follows: continue;

Sample Implementation:

public class myTest {
  public static void main(String args[]) {
    int[] mynumbers = {
      0,
      5,
      10,
      15,
      20
    };
    for (int i: mynumbers) {
      if (i == 15) {
        continue;
      }
      System.out.print(i);
      System.out.print(“\n”);
    }
  }
}

The expected output of the code is:

0
5
10
20