Learn Fundamentals of Java Programming “The for statement in Java Programming ” Lesson 14
The for statement
The for loop is the most widely used Java loop construct. The structure of the Java for statement is as below:
for (counter = initial_value ; test_condition ; increment/decrement counter)
{
statements
}
Semicolons separate the three parts of a for loop:
1. The initial_value initializes the value of the loop counter.
2. The test_condition tests whether the loop should be executed again. The loop is exited when the test condition fails.
3. The step updates the counter in each loop iteration.
For example, consider the following loop that prints the square of numbers from 1 to 5:
for (int number = 1; number <= 5 ; ++number )
{
System.out.print(“Square of ” + number);
System.out.println(” = ” + number*number);
}
When the loop begins, the variable number is set to 1. Then the test_condition tests whether the loop variable, number <=5. If it is, the body of the loop is executed – the square of 1 is printed. The step then increments count by 1. The test_condition is tested again to decide whether to execute the body. If it is less than 5, the square of the variable number is printed. When number exceeds 5, the loop is exited.
In English, we read the loop above as – for number is equal to 1; number is less than or equal to 5; execute the body of the loop; increment number; loop back to the test condition.
The loop given above counts up the loop index – it is an incrementing loop. We can also count down in a loop for a decrementing loop. The following decrementing loop will execute 5 times.
for (int number = 5; >= 1; n umber -1)
{
System.out.print(“Square of ” + number);
System.out.println(” = ” + number*number);
}
The loop index can be incremented or decremented by any value, not just 1. The following loop increments the loop index by 2. It displays all odd numbers between 1 and 20.
for ( int count = 1; <= 20; +2)
{
System.out.println(count) ;
}
The loop index can begin with any value, not necessarily 1. The following loop also iterates 5 times and prints number from 5 to 9.
for ( int count = 5 ; count < 10 ; count++)
{
System.out.println(count);
}
The counter in the loop iterates with values – 5,6,7,8,9 – for a total of five times. Notice that the test condition is count < 10 and not count <= 10. If it was the latter, the loop would have iterated 6 times for loop index as – 5,6,7,8,9,10.
STYLE TIP: For LOOP
1. If there is only one statement in the body of the loop, the set of curly braces enclosing the body can be omitted. For example,
for ( int count = 5; count < 10; count++)
System.out.println(count) ;
2. You can use multiple items in the initialization and the updation part of the loop by separating them with the comma operator.
For example,
for ( x = 0, y 10; < x++, for ( x = 0, y 10; < x++, y– )
System.out.println( “x = “ + x + “y = “ + y) ;
3. Do remember to use indentation to align the body of the loop, it will make it easier for you to debug your code.
Common Coding Errors: The for Loop
1. Initial value is greater than the limit value and the loop increment is positive.
For example,
for ( int count = 5; <= 1; count++)
In this case, body of the loop will never be executed
2. Initial value is lesser than the limit value and the loop increment is negative.
For example,
for ( int count = 1; >= 5; count = 1; >= 5; — )
In this case also, body of the loop will never be executed.
3. Placing a semicolon at the end of a for statement:
for ( int count = 1; <= 5; count++) ;
{
//body of loop
}
This has the effect of defining the body of the loop to be empty. The statements within the curly braces will be executed only once (after the for loop terminates) and not as many times as expected.
4. Executing the loop either more or less times than the desired number of times.
For example, the following loop iterates 4 times and not the intended 5 times because it exits when count = 5.
for ( int count = 1; < 5; ++)
The correct way to loop five times would be to test for count <= 5.
Such errors are known as off by one errors.
5. Using a loop index (declared within the loop) outside the loop.
for ( int count = 1; < 5; ++)
{
System.out.println(count);
}
System.out.println(count); //error!!
The scope of the variable count is only within the body of the loop. It is not visible outside the loop.