The for loop in Java is an entry-controlled loop. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The three components of the for loop (separated by ;) are variable declaration/initialization (here int i = 0), the condition (here i < 100), and the increment statement (here i++).
- The variable is initialized once at the beginning.
- The condition is checked before each iteration.
- If the condition is true, the loop body runs.
- After the body, the increment statement runs.
- Then the condition is checked again.
- When the condition is false, the loop stops.
The curly braces are optional (you can one line with a semicolon) if the loop contains just one statement. But, it's always recommended to use braces to avoid misunderstandings and bugs. The for loop components are optional. If your business logic contains one of these parts, you can omit the corresponding component from your for loop.
Code Snippet
//For Loop in Java
import java.util.Scanner;
public class App {
public static void main(String args[]) {
System.out.println("Enter The Limit : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
}