Monday, 12 January 2015

// The "Chapter3_Example4Bii" class.  Chapter 3, Example 4Bii.
// (from the Carter book)

// If we want to have the ELSE associated with the first IF, the preferred
// solution is to use brace brackets to "close off" the second IF, as shown
// below.  This works if 'p' and 'q' are both set to true, but not if 'q'
// is set to false (while 'p' is true).  If 'p' is false, it works, as well.

// An empty ELSE statement provides an alternative solution to the problem.
// In this solution, no brace brackets are required.

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

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

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

        boolean p = true, q = true;
        if (p)
            if (q)
                c.println ("Take Path 1A");
            else
                ;  // to "close off" the second IF
        else
            c.println ("Take Path 2A");

        p = false;
        q = false;

        if (p)
            if (q)
                c.println ("Take Path 1B");
            else
                ;  // to "close off" the second IF
        else
            c.println ("Take Path 2B");


      } // main method
} // Chapter3_Example4Bii class

No comments:

Post a Comment