Monday, 12 January 2015

// The "Chapter3_Example1" class.  Chapter 3, Example 1.
// Nested IF statements.  (from the Carter book)

// For any value of x, this solution involves 3 tests.  But, we can
// increase the efficiency of the code by nesting these statements
// (see Example 2, which involves placing one IF statement within
// another IF statement).

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

public class Chapter3_Example1
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        
        int x = -10;
        
        if (x < 0)
        {
            c.println ("x value is negative");
        }
        else if (x > 0)
        {
            c.println ("x value is positive");
        }
        else
        {
            c.println ("x value is zero");
        }
     
    } // main method
} // Chapter3_Example1 class

No comments:

Post a Comment