Page 189 - PowerPoint Presentation
P. 189

CAVITE STATE UNIVERSITY
                               T3 CAMPUS
                               Department of Information Technology          COSC 65 – Programming Languages

               Java Nested if Statement
                       The nested if statement represents the if block within another if block. Here, the inner
               if block condition executes only when outer block condition is true.

                  //Syntax                                                                     False
                  if (condition) {                                                 Condition
                        // code to be executed
                        If (condition) {                                         True
                        // code to be executed                                      If code
                                         }
                                  }
                                                                                             False
                  // Java program to demonstrate the Nested if                     Condition
                  public class NestedifExample{
                        public static void main (String[] args) {                True
                  int age=20, weight=51;
                                                                                    If code
                  if(age>=18) {
                          if (weight>50) {
                        System.out.println(“You are allowed to donate blood”);     Statement
                              }                }
                  } }


                  // Java program to demonstrate the Nested if
                  public class NestedifExample2{
                        public static void main (String[] args) {
                  int age=20, weight=51;
                  if(age>=18) {
                        if (weight>50) {
                        System.out.println(“You are allowed to donate blood”);
                              }
                        else {
                        System.out.println(“You are not allowed to donate blood”);
                  }
                  }
                  else {

                        System.out.println(“Age must be greater than 18 years old!”);
                  }
                  }}
















                                                                                                 Page | 48
   184   185   186   187   188   189   190   191   192   193   194