© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_8

8. Loop Structure – for Loop

Ron Dai1 
(1)
Seattle, WA, USA
 
Simply put, the loop structure repeatedly does something until the state is changed (see Figure 8-1).
../images/485723_1_En_8_Chapter/485723_1_En_8_Fig1_HTML.png
Figure 8-1

The for loop structure

Example

Here is an example:
        for (int i = 0; i < 100; i++) {
               <do something>
        }
There are three key elements in the for statement:
  • int i = 0: declare a counter variable and assign an initial value to it;

  • i < 100: define the condition to continue with the for loop; as long as this condition is true the loop will run, when it is not true, we stop and exit from the for loop;

  • i++: increment the counter value; i++ is the same thing as i = i + 1.

So, how many times will the “<do something>” be repeated in the code above?

Lab Work

  1. 1.

    Use the for loop to output “Hello!” 10 times.

     
  2. 2.

    Use the for loop to print out all integers from 1 to 25, inclusive.

     
  3. 3.

    Print out all integers from 1 to 25.

     
  4. 4.

    Output all even numbers from 3 to 99.

     

The for Loop Formula

The math model behind the for loop is actually an arithmetic sequence:
for (int counter=firstTerm;
       counter <= lastTerm;
       counter=counter + difference) {
        ......
}
The nth term in the counter series is equal to:
  • firstTerm + difference × (n – 1)

Finding the “for Loop” Formula for an Arithmetic Sequence

As an example, here is a list of numbers:
  • -4, 5, 14, 23, 32, 41, 50, 59, 68, 77, 86.

  • It follows an arithmetic sequence.

  • firstItem = -4

  • lastItem = 86

  • difference = 5 – (-4) = 9

  • Translate this to a for loop:

for(int i=-4; i <= 86; i=i+9) { ...... }

It will iterate through every single number in the list.

Math: Counting Strategically

You may finger count, but that will not work when you have an extremely large amount of numbers in the series. The right approach is to prepare these numbers by reorganizing them. The purpose is to find a good pattern so that we can count systematically.

Finding a pattern here is to figure out a basic formula representing every number in the series.

Look at this example: 3, 4, 5, 6, ...., 100, so we know the total count of numbers is:
  • 100 – 3 + 1 = 98.

A common method is to convert the number series to something more straightforward. If we subtract 2 from every number in the series, we get 1, 2, 3, 4, ......, 98. We now know the count is 98. And, the formula representing every number will be x(i) = i + 2, (i=1, 2, ......,98).

What about 5, 8, 11, 14, ......, 101? How do you use the “for loop” to print it out?

It looks more complicated than the previous one, but you can try the same approach.
  1. 1.

    Subtract 5 from every number; it becomes 0, 3, 6, 9, ......, 96

     
  2. 2.

    Divided by 3, it then becomes 0, 1, 2, 3, ......, 32

     
  3. 3.

    It is not hard to count from 0, one by one up to 32. The total count is 33.

     
  4. 4.

    The general term for the i-th number in the series will be x(i) = 3 * i + 5, (i=0, 1, 2, ......, 32).

     
Now, go back to the for loop construction, and it is obvious the answer should be something like this:
        for (i=0; i <= 32; i++) {
               System.out.println(3 * i + 5);
        }

Lab Work

  • Write a for loop to produce the following list of numbers:

    1 4 9 16 25 36 49 64 81 100

    (Hint: watch for a common pattern.)

Example

What is the output of the following sequence of loops?

../images/485723_1_En_8_Chapter/485723_1_En_8_Figa_HTML.jpg
It prints out the following:
****!****!****!
****!****!****!

The external for loop (marked as “1”) has two iterations, so the output will have two lines, by println().

The middle for loop (marked as “2”) has three iterations, so it will print out 2 x 3 = 6 “!” in total by print().

The internal for loop (marked as “3”) has four iterations, so it will print out 2 x 3 x 4 = 24 “*” in total by print().

Lab Work

  • Write a method exp() to compute an exponential result, given the input of a base number and a power (a.k.a. an exponent number). For example, exp(3, 4) returns 81. The restriction is that the base and exponent numbers are non-negative.

Problems

  1. 1.
    What is the output of the following sequence of loops?
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 3; j++) {
            System.out.print(i + ""*"" + j + ""= "" + i * j + "";  ");
        }
        System.out.println();
    }
     
  2. 2.

    Write a for loop to produce the following list of numbers:

    5 10 17 26 37 50

     
  3. 3.

    Write a for loop to produce the following list of numbers:

    1 8 27 64 125

     
  4. 4.

    Write a for loop to produce the following list of numbers:

    -1 0 7 26 63 124

     
  5. 5.
    Use for loops to produce the following output:
    *****
    *****
    *****
    *****
     
  6. 6.

    Write for loop code to output the following:

    1

    22

    333

    4444

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.221