Turn Desktop View Off
by Dinesh Thakur

When we have if or if else statement within another if construct or else construct, we call it nested if.

Syntax:

if(condition)

   {

        if(condition)

                statement;

               [else statement; ]

  }

       else

              {

                         if(condition)

                                        statement;

                                       [else statement;]

              }

As, we can see in above syntax that else is optional (enclosed within []). We can have any number of if else within if or else section i.e. we can have if else statements nested to any level.

Secondly, in nested if statements, the internal if else statements in if section will be evaluated only in case the logical expression of outer if statement evaluates to true. Similarly, the if else statements of else section will be checked for validity only if the logical expression of outer if statement evaluate to false.

/* Enter three values a,b and c and print largest of them  */

import java.io.*;

class Largest

{

            public static void main(String args[] ) 

            throws IOException

            {

                        BufferedReader k=new BufferedReader(new InputStreamReader

                                    (System.in));

                        String h;

                        int a,b,c;

                        System.out.print("Enter three Values :");

                        h=k.readLine( );

                        a=Integer.parseInt(h);

                        h=k.readLine( );

                        b=Integer.parseInt(h);

                        h=k.readLine( );

                        c=Integer.parseInt(h);

                        if(a>=b)

                        {

                                    if(a>=c)

                                    {

                                                System.out.println("Largest is "+a);

                                    }

                                    else

                                    {

                                                System.out.println("Largest is "+c);

                                    }

                        }

                        else

                        {

                                    if(b>=c)

                                    {

                                                System.out.println("Largest is "+b);

                                    }

                                    else

                                    {

                                                System.out.println("Largest is "+c);

                                    }

                        }

            }

}