dimanche 13 décembre 2015

What is printed when the following myProgram() method in the SimpleIsland class is executed?

While I'm aware of how if/else statements and operators work (there are many posts about it), I seem to have gotten lost within this block of code in particular. I was given the final output (if else if), however I haven't been able to get to that conclusion by simply looking at it and working it out on paper. I'd really appreciate an explanation in plain terms as to what's happening in the code.

public class SimpleIsland
{
    public boolean getTrue()
    {
        return true;
    }
    public boolean getFalse()
    {
        return false;
    }
    public static void myProgram()
    {
        if ( getFalse() || getTrue() )
        {
            if ( !getTrue() )
            {
                if ( !getFalse() )
                {
                    System.out.println( "if if if" );
                }
                else 
                {
                    System.out.println( "if if else" );
                }
            }
            else 
            {
                if ( !getFalse() )
                {
                    System.out.println( "if else if" );
                }
                else 
                {
                    System.out.println( "if else else" );
                }
            }
        else
        {
            System.out.println( "else" );
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire