Java for Loop Statement

The for loop in Java is a control flow statement that allows you to repeat a block of code a specified number of times. It’s a looping statement that allows you to execute a block of code repeatedly until a particular condition is met. The for loop is useful when you know in advance how many times you need to loop, as it allows you to specify the number of iterations in the loop control.

Java For Loop

The for loop in Java is a control flow statement that allows you to repeat a block of code a specified number of times. It’s a looping statement that allows you to execute a block of code repeatedly until a particular condition is met. The for loop is useful when you know in advance how many times you need to loop, as it allows you to specify the number of iterations in the loop control.

The syntax of the for loop is straightforward and consists of a for keyword followed by three optional expressions in parentheses, and a block of code in curly braces. The three optional expressions are separated by semicolons and are as follows:

  1. The initialization expression is executed before the loop starts and is used to initialize the loop variable. This expression can be a single statement or a comma-separated list of statements. It can also declare a loop variable, which is a local variable that is visible only within the loop. The loop variable is initialized to a starting value and is updated at the end of each iteration.
  2. The termination expression is a boolean expression that is evaluated before each iteration of the loop. If the termination expression is true, the loop continues. If the termination expression is false, the loop is terminated and control is transferred to the next statement following the loop. The termination expression is optional and can be omitted if you want to create an infinite loop.
  3. The increment expression is executed at the end of each iteration and is used to update the loop variable. This expression can be a single statement or a comma-separated list of statements. It can also be omitted if you don’t need to update the loop variable.

Here is the general syntax of a for loop in Java:

for (initialization; termination; increment) {
  // code block to be executed
}

Here is an example of a for loop in Java that counts from 1 to 10:

for (int i = 1; i <= 10; i++) {
  System.out.println(i);
}

In this example, the initialization expression is int i = 1, which declares a loop variable i and initializes it to 1. The termination expression is i <= 10, which means that the loop will continue to execute as long as i is less than or equal to 10. The increment expression is i++, which increments i by 1 at the end of each iteration. The code block inside the loop consists of a single statement that prints the value of i to the console.

The output of this for loop will be the numbers 1 through 10, each on a separate line.

Here is another example of a for loop in Java that calculates the factorial of a number:

int num = 5;
int factorial = 1;

for (int i = num; i > 1; i--) {
  factorial *= i;
}

System.out.println(factorial); // 120

In this example, the initialization expression is int i = num, which declares a loop variable i and initializes it to the value of num, which is 5. The termination expression is i > 1, which means that the loop will continue to execute as long as i is greater than 1.

Similar Posts

Leave a Reply

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