Java

TOPIC 23 – FOR LOOPS

 

 

LESSON NOTE

 

 

FOR VS WHILE LOOPS

 

If the amount of iterations can be calculated before the loop begins, then it is generally a good idea to use a FOR loop instead of a WHILE loop.

 

Note that this does not mean that you need to calculate the amount of iterations.  It just has to be something that can be calculated.


FOR LOOP TEMPLATE

 

Here is the for loop template:

 

for (initialization; condition; update)

{

   //statement block

}

 

The initialization section is where one declares and initializes the control variable.

The condition section is where the condition is located. Before every execution of the statement block, this condition is checked. If it is true, the statement block is executed, if it is false, the statement block is skipped and the loop is done.

The update section is where one should update the control variable.

 

EXAMPLES

 

Example 1 – Output all numbers from 1 to 1000 to screen.

 

for (int x = 1; x <= 1000; x++)

{

   System.out.println(x);

}

 

Example 2 – Output all numbers between 1 and 25000 that are divisible by 3.

 

          Solution #1

 

            for (int a = 1; a <= 25000; a++)

     {

         if (a % 3 == 0)

         {

           System.out.println(a);

   }

}

 

            Solution #2

 

            for (int a = 3; a <= 25000; a = a + 3)

     {

         System.out.println(a);

}

 

Example 3 – Write a program that will ask the user for a number representing rows and the program will then output a rectangle made of asterisks with that many rows.  There should be 10 columns to the rectangle.

 

Sample output 1:

 

     How many rows?
     3

 

     **********
     **********
     **********

 

Sample output 2:

 

     How many rows?

     6

 

     **********
     **********
     **********

     **********
     **********
     **********

 

Solution:

 

     System.out.println("How many rows?");

     Scanner scr = new Scanner(System.in);

     int rows = scr.nextInt();

 

     for (int r = 1; r <= rows; r++)

     {

        System.out.println("**********");

     }

 


ADVANCED EXAMPLE

 

Example – Consider the previous example where the program had to output a rectangle of asterisks.  This time, the program needs to output a square of asterisks.  So the number of rows and columns will be the same.

 

Sample output 1:

 

     What is the size of square?
     3

 

     ***
     ***
     ***

 

Sample output 2:

 

     What is the size of square?

     6

 

     ******
     ******
     ******

     ******
     ******
     ******

 

Solution:

 

System.out.println("What is the size of square?");

Scanner scr = new Scanner(System.in);

int size = scr.nextInt();

 

for (int r = 1; r <= size; r++)

{

   for (int c = 1; c <= size; c++)

   {

      System.out.print("*");  //add a star

   }

   System.out.println("");   //change lines

}

 

GOOD PROGRAMMING PRACTICE

 

The control variable should only be changed in the update section.

COMMON ERROR

 

One common error that is not detected by the Java Compiler is placing a semicolon at the end of the line with:

 

for (initialization; test; update)

 

In such a situation, the compiler thinks that the semicolon is the actual statement block. Essentially, your loop executes nothing several times.

 

FOR LOOPS TO WHILE LOOPS

 

All for loops can always be replaced by while loops. Here is how the for loop template would be coded in a while loop:

 

For loop template

While loop

for (initialization; condition; update)

{

   //statement block

}

initialization;

while (condition)

{

   //statement block

   update;

}