Wednesday, 3 December 2014

// The "NestingLoopStructures1" class.  Page 152.
// (from the Carter book)

// Example 1.  The following fragment prints a triangle
// of asterisks with the size of the triangle determined
// by the value of the 'numberOfRows' variable, for example,
// 7 rows of asterisks.  Nested for loops are used for doing
// this task.


public class NestingLoopStructures1
{
    public static void main (String[] args)
    {
        int numberOfRows = 10;
        // the outer for loop is for the number of rows of asterisks
        for (int row = 1 ; row <= numberOfRows ; row++)
        {
            // the inner for loop is for the position of each asterisk in each row
            for (int position = 1 ; position <= row ; position++)
            {
                // use 'System.out.print' for items that are all on the same line
                System.out.print ("*");
            }
            // use 'System.out.println()' for making a blank line
            System.out.println ();
        }
    } // main method
} // NestingLoopStructures1 class

No comments:

Post a Comment