Page 183 - Computer science 868 Class 12
P. 183

Output:
                 Selected for Science

                 nested if statement
                 When an if statement is placed inside another if statement, then it is known as a nested if statement.

                 Syntax:
                    if(condition 1)
                    {
                        Statement 1;
                    if(condition 2) // nested if
                    {
                        Statement 2;
                        }
                    }
                    else
                    {
                    if(condition 3)
                    {
                        Statement 3;
                        }
                    }

                 For example:
                 Accept three numbers from the user and print the greatest number.

                    import java.util.*;
                    class greatest
                    {
                        public static void main()
                        {
                            Scanner sc = new Scanner(System.in);
                            int num1, num2, num3;
                            System.out.print("Input the 1st number: ");
                            num1 = sc.nextInt();
                            System.out.print("Input the 2nd number: ");
                            num2 = sc.nextInt();
                            System.out.print("Input the 3rd number: ");
                            num3 = sc.nextInt();
                            if(num1 > num2)
                                if(num1 > num3)
                                    System.out.println("The greatest number is : " + num1);

                            if(num2 > num1)
                                if(num2 > num3)
                                    System.out.println("The greatest number is : " + num2);

                            if(num3 > num1)
                                if(num3 > num2)
                                    System.out.println("The greatest number is : " + num3);
                        }
                    }




                                                                                                                       181
                                                                                                 Statements and Scope  181
   178   179   180   181   182   183   184   185   186   187   188