Nested if Statement

1. Assume that int type variables a and b have values a=2 and b=3.
Determine the outcome in each of the following peaces of code.

  1. if (a<b)
    if (a==2)
    System.out.println( "One");
    else
    System.out.println( "Two");
    else
    System.out.println( "Three");
    System.out.println( "Four");

    1. Solution



  2. if (a<b)
    if (a==4)
    System.out.println( "One");
    else
    System.out.println( "Two");
    else
    System.out.println( "Three");
    System.out.println( "Four");

    2.Solution



  3. if (a>b)
    if (a==2)
    System.out.println( "One");
    else
    System.out.println( "Two");
    else
    System.out.println( "Three");
    System.out.println( "Four");

    3. Solution


  4. if (a>b)
    if (a==4)
    System.out.println( "One");
    else
    System.out.println( "Two");
    else
    System.out.println( "Three");
    System.out.println( "Four");


    4. Solution