// Example 2.2 joi/examples/While2Demo.java
//
//
// Copyright 2003 Bill Campbell and Ethan Bolker

// A class for illustrating the while-statement.  A typical run:
// 
// %> java While2Demo
// Enter integer: 10
// Fibonacci numbers <= 10: 1 1 2 3 5 8
// Fibonacci numbers <= 10: 1 1 2 3 5 8
// First 10 Fibonacci numbers: 1 1 2 3 5 8 13 21 34 55

public class While2Demo 
{
    public static void main( String[] args ) 
    {
        Terminal terminal = new Terminal();  // for input and output

        // Prompt for and read a single integer.
        int n = terminal.readInt( "Enter integer: " );
        
        // while tests a condition
        terminal.print( "Fibonacci numbers <= " + n + ":" );
        int thisOne = 1;
        int lastOne = 1;
        while ( lastOne <= n ) {
            terminal.print( " " + lastOne );
            int nextOne = thisOne + lastOne;
            lastOne = thisOne;
            thisOne = nextOne;
        }
        terminal.println();

        // while tests a boolean variable
        terminal.print( "Fibonacci numbers <= " + n + ":" );
        thisOne = 1;
        lastOne = 1;
        boolean more = true;
        while ( more ) {
            if ( lastOne > n ) {
                more = false;
            } 
            else {
                terminal.print( " " + lastOne );
                int nextOne = thisOne + lastOne;
                lastOne = thisOne;
                thisOne = nextOne;
            }
        }
        terminal.println();

        // while used for counting
        terminal.print( "First " + n + " Fibonacci numbers:" );
        thisOne = 1;
        lastOne = 1;
        int i = 1;
        while ( i <= n ) {
            terminal.print( " " + lastOne );
            int nextOne = thisOne + lastOne;
            lastOne = thisOne;
            thisOne = nextOne;
            i++;  // same as 'i = i + 1;'
        }
        terminal.println();
    }
}
