Page 252 - PowerPoint Presentation
P. 252

CAVITE STATE UNIVERSITY
                               TRECE MARTIRES CITY CAMPUS
                               Department of Information Technology            DCIT 111 - Advanced Programming

               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!”);
                  }
                  }}

















                                                                                                            28
   247   248   249   250   251   252   253   254   255   256   257