for loop in java



If you have closely looked at the while loop that we discussed in previous post you must have noticed that every while loop has three component

1. A variable that is initialized before the start of the while loop
2. The variable ( or possibly an expression involving that variable) is checked at every loop.
3. The value of the variable is updated inside the while loop.


Here is the while loop that was presented in previous post

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.   while loop example
  4.  */
  5. class whileexample{
  6. public static void main (String args[]) {
  7. int i;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10. i =1 ;
  11. System.out.println("Inch cm ");
  12. while (i<=10)
  13. {
  14. cm = i*2.54 ;
  15. System.out.print( i );
  16. System.out.println(" "+cm );
  17. i++;
  18. }
  19. }
  20. }


1. First the value of i is initialized by one using statement i =1;
2. The value of i is checked at each iteration of while loop with statement while (i<=10)
3. The value of i is updated using i++;


A for loop combines the three functionality in a single line of code. Look at the same program written using for loop.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   for loop example
  4.   */
  5. class forloop{
  6. public static void main (String args[]) {
  7. int i;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10.  
  11. System.out.println("Inch cm ");
  12. for ( i=1; i<=10; i++)
  13. {
  14. cm = i*2.54 ;
  15. System.out.print( i );
  16. System.out.println(" "+cm );
  17. }
  18. }
  19. }


If you compile and run this program, you will get the same output output as follows


C:\Program Files\Java\jdk1.7.0_17\bin>java forloop
Inch cm
1 2.54
2 5.08
3 7.62
4 10.16
5 12.7
6 15.24
7 17.78
8 20.32
9 22.86
10 25.4




Formally, for construct is defined as

  1. for(initialization; Boolean_expression; update)
  2. {
  3. //A list of Statements
  4. }


- At the start of the for loop the initialization step is executed. In the example above the initialization step assigned value 1 to i.

- The Boolean expression is evaluated in the next step and if found true, the body list of statements inside the for loop is executed. If the Boolean expression is false, the list of statements are not executed and the control jumps out of the for loop.

After the execution of the for statements ( if they do), the updtae statement(s) is ( are) executed. The Booleas expression is evaluated again and if found true, the loop executes and the process repeats itself (body of loop, then update step,then Boolean expression). After the Boolean expression is false, the for loop terminates.

Special considerations



1. It is possible to omit the initialization step in the for loop ( if initialization steps have been performed before the start of the for loop. You still need to inlcude the ; the for loop. The following example performs the same task.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   for loop example - omit initialization step
  4.   */
  5. class forloop1{
  6. public static void main (String args[]) {
  7. int i;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10.  
  11. i=1;
  12. System.out.println("Inch cm ");
  13. for (; i<=10; i++)
  14. {
  15. cm = i*2.54 ;
  16. System.out.print( i );
  17. System.out.println(" "+cm );
  18. }
  19. }
  20. }


2. It is possible to omit the update statemets (if it has possibly been included inside the for statements. The following example is equivalent.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   for loop example - omit update statement
  4.   */
  5. class forloop2{
  6. public static void main (String args[]) {
  7. int i;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10. System.out.println("Inch cm ");
  11. for (i=1; i<=10;)
  12. {
  13. cm = i*2.54 ;
  14. System.out.print( i );
  15. System.out.println(" "+cm );
  16. i++;
  17. }
  18. }
  19. }


2. It is possible to include more than one initialization ot update statements in a for loop. These statememts should be separated by comma. The following example shows the how we can initialize an update another variable x iniside the for loop.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   for loop example - multiple initilization and update statements
  4.   */
  5. class forloop2{
  6. public static void main (String args[]) {
  7. int i,x;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10. System.out.println("Inch cm ");
  11. for (i=1, x = 1; i<=10; i++, x++)
  12. {
  13. cm = i*2.54 ;
  14. System.out.print( i );
  15. System.out.println(" "+cm );
  16. if (x == 5) System.out.println();
  17. }
  18. }
  19. }



Exercises


Exercise 1
Rewrite the example such that the inch cm table is printed in reverse order from 10 inch to 1 inch