Thursday, 18 December 2014

// The "Exercise44B_Chapter4" class.
// (from the Carter book)

// Exercise 4.4 - Question 2 (a & b):

// Rewrite each fragment as a single FOR statement whose body
// consists of only an empty statement (as illustrated in Example 2
// in which only a semi-colon appears in the body of the FOR loop).

import java.awt.*;
import hsa.Console;

public class Exercise44B_Chapter4
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        // Question 2 (a):  (rewritten FOR statement)
        // will print the products of 'i':

        for (int product = 1, i = 10 ; i > 0 ; c.println (product),
                product *= i, i--)
            ;
        c.println ();

        // Question 2 (b):

        for (int i = 0, max = 0, j = 10 ; i < 10 || (i * j > max) ;
                max = i++ * j--,
                c.println (max))
                   ;

    } // main method
} // Exercise44B_Chapter4 class

No comments:

Post a Comment