Java

TOPIC 24 – FOR LOOPS, PART 2

 

 

LESSON NOTE

 

 

NESTED LOOPS

 

It is often necessary to have one loop inside the other.  This is called nested loops. 


SIMPLE NESTED LOOPS

In some cases, when the loops are independent from one another, the code is fairly simple to create and understand.

 

EXAMPLE

 

This is an example of a simple nested FOR loop program.  It is done in two parts to show you how it is built up.

 

PART 1

 

First, create the program that uses a loop to output the numbers 1 to 15 on a single. 

 

SOLUTION

    for(int n=1; n<=15; n++)

    {

       System.out.print(n + ",");

    }

    System.out.println("");



The above code will output:

 

            1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,

 

Note that the 2nd println statement is simply there to bring the cursor to the next line.

 

PART 2

 

Write the program that will output numbers 1 to 15 on the same line and do that 5 different times.

 

PSEUDOCODE SOLUTION

 

    Loop five times

    {

         Output numbers from 1 to 15 on same line

    }


The "loop five times" is a simple for loop.  And the "output numbers" line that is inside the loop is the solution from part 1.  Putting this all together, we get:

 

    for (int c=0; c<5; c++)

    {

       for(int n=1; n<=15; n++)

       {

           System.out.print(n + ",");

       }

       System.out.println("");

    }

 

The above will output:

 

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,


 

ADVANCED NESTED LOOPS

Nested loops often appear confusing and fairly complicated.  This is especially true for nested loops where the amount of times that the inner loops iterates depends on the control variable of the outer loop.

 

EXAMPLE

 

Write a program that will output a triangular star pattern like below using loops:

 

*

**

***

****


PSEUDOCODE SOLUTION

    Loop for each row
    {

       Output the number of stars required

    }


LOOP TABLE

This loop table will allow us to figure out all the values that are needed.  You may find it easy to do this without the table for now, but when things get complicated, the table is very useful.

 

Row #

(row)

Stars
(stars)

1

1

2

2

3

3

4

4


From the table, we can conclude that on any row, the amount of stars is equal to the row's number.

SOLUTION

 

       for (int row=1; row<=4; row++)

       {

           for(int star=1; star<=row; star++)

           {

               System.out.print("*");

           }

           System.out.println("");

       }


The program above will output:

 

*

**

***

****

 

 

 


EXAMPLE 2


Write program that will ask the user to enter an amount of sequences and will the first five numbers of the sequences as shown below:

 

    How many sequences?

    4

    Here we go:
    1, 2, 3, 4, 5,

    2, 4, 6, 8, 10,

    3, 6, 9, 12, 15,

    4, 8, 12, 16, 20,

 

PSEUDOCODE SOLUTION

 

    Ask user for number of sequences.
    Get that value s from user.

 

    Loop s times

    {

        Output sequence

    }

 

PSEUDOCODE FOR "OUTPUT SEQUENCE"

 

            The line "Output sequence" is very simple above but is fairly complex to implement.

 

    Set num to first value

    Loop 5 times

    {

        Display num

        Increase num by first value

    }
    Go to next line.

 

FULL PSEUDOCODE

The above pseudocode does need to be altered a little.  The first value of a sequence constantly changes from one row to another.  It is important to realize that it is in fact the row number.  Also, the sequence grows by an amount that is also equal to the row number. 


    Ask user for number of sequences.
    Get that value s from user.

 

    Loop s times

    {

       Set num to the row number

        Loop 5 times

        {

            Display num

            Increase num by row number

        }
       Go to next line.

    }

 

SOLUTION

    Scanner scr = new Scanner(System.in);

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

    int seq = scr.nextInt();

      

    for (int row=1; row<=seq; row++)

    {

        int num=row;

        for(int numbers=1; numbers<=5; numbers++)

        {

           System.out.print(num + ", ");

           num = num + row;

        }

        System.out.println("");

    }