// This file collects simple examples of the Java flow control constructs
// (if, if-else, while, for) that we have covered -- M. Branicky, 10/07/06

public class SimpleFlow
{ // begin of class SimpleFlow

    public static void main(String args[])
    { // begin of main method

        /////////////////////////// SIMPLE if ////////////////////////////

        // if PATTERN: 
        //    if (boolean_expression_to_test)
        //    {
        //       // if block: one or more statements here
        //    }

        if (true)
        { // begin if
            // this WILL PRINT because if-expression has (constant) value true
            System.out.println("(1) if block: prints if if-expression is true.");
        } // end if

        if (false)
        { // begin if
            // this WILL NOT PRINT because if-expression has (constant) value false
            System.out.println("(2) if block: prints if if-expression is true.");
        } // end if

        int num = 3;
        if (num<5)
        { // begin if
            // this WILL PRINT because if-expression has value true
            System.out.println("(3) if block: prints if if-expression is true.");
        } // end if

        if (num>=5)
        { // begin if
            // this WILL NOT PRINT because if-expression has value false
            System.out.println("(4) if block: prints if if-expression is true.");
        } // end if


        /////////////////////////// SIMPLE if-else ////////////////////////////

        // if-else PATTERN: 
        //    if (boolean_expression_to_test)
        //    {
        //       // if block: one or more statements here
        //    }
        //    else
        //    {
        //       // else block: one or more statements here
        //    }

        if (true)
        { // begin if
            // this WILL PRINT because if-expression has (constant) value true
            System.out.println("(5) if block; prints if if-expression is true.");
        } // end if
        else
        { // begin else
            // this WILL NOT PRINT because if-expression has (constant) value true
            System.out.println("(6) else block: prints if if-expression is false.");
        } // end else

        if (num>=5)
        { // begin if
            // this WILL NOT PRINT because if-expression has value false
            System.out.println("(7) if block: prints if if-expression is true.");
        } // end if
        else
        { // begin else
            // this WILL NOT PRINT because if-expression has value false
            System.out.println("(8) else block: prints if if-expression is false.");
        } // end else


        /////////////////////////// SIMPLE while ////////////////////////////
        
        // while PATTERN: 
        //    // initialize loop variables here
        //    while (boolean_expression_to_test)
        //    {
        //       // one or more statements here
        //       // update loop variables here
        //    }

        // declare a loop variable for later use in while and for loops below
        int counter;

        // intialize loop variable
        counter = 0;
        while (counter<5)
        { // begin while
            // this WILL PRINT 5 times
            System.out.println("(9) while block: prints while while-expression is true.");
            // update loop variables
            counter = counter + 1;  // counter+=1; OR counter++; WOULD HAVE BEEN EQUIVALENT
        } // end while


        /////////////////////////// SIMPLE for ////////////////////////////
        
        // for PATTERN: 
        //    for ( initialization_part; boolean_expression_to_test; update_part)
        //    {
        //       // one or more statements here
        //    }

        // this is equivalent to the while loop above
        // NOTE: no "int" before counter because it is already declared above
        for (counter = 0; counter<5; counter++)
        { // begin while
            // this WILL PRINT 5 times
            System.out.println("(10) for block: prints while test-expression is true.");
        } // end while

    } // end of main method

} // end of class SimpleFlow
