ABC of Programming
About Lesson

The for loop statement is a fundamental loop construct in programming that is used to execute a block of code a specified number of times. It is particularly useful when you know the exact number of iterations you want to perform.

The syntax of the for loop statement is as follows:

for (initialization; condition; increment/decrement) {
// code to be executed

Here’s what each part of the for loop syntax means:

  • initialization: This is the initialization statement, which is executed once before the loop starts. It is used to initialize the loop counter variable.
  • condition: This is the loop continuation condition. It is evaluated at the beginning of each iteration, and the loop continues as long as the condition is true.
  • increment/decrement: This is the update statement. It is executed at the end of each iteration and is used to update the loop counter variable.

Here’s an example of a simple for loop in Java that prints the numbers 0 to 9:

for (int i = 0; i < 10; i++) {

In this example, the initialization statement initializes the loop counter variable i to 0, the condition is i < 10, and the increment statement is i++, which increments i by 1 after each iteration. The loop will execute 10 times, with i taking on the values 0, 1, 2, …, 9.

The for loop is a powerful construct that can be used in a variety of scenarios. For example, it can be used to iterate over the elements of an array or a collection using the for-each loop.

Join the conversation