Page 251 - PowerPoint Presentation
P. 251

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

                                                     //Java program to demonstrate the use of if-else

                       condition   False              statement
                                                     // it is a program of identifying if the number is either 1
                       True                           or 0
                         if code       else code
                                                     public class ifElseExample {
                                                            public static void main(String[] args) {
                       statement      statement      int num=1;

                                                     if(num=1) {
                                                            System.out.println(“The Number is 1”);
                                                     }
                                                     else {
                                                            System.out.println(“The Number is 0”);
                                                            }
                                                     } }                               Output:
                                                                                       The Number is 1

               Java if-else-if ladder Statement
                       The if-else-if ladder statement executes one condition from multiple statements.
                                                              /* Java program to demonstrate if-else-if
                  //Syntax                                    statement
                  if (condition_1) {                          It is a program to display a mark
                    }    // code if condition_1 is true       */
                  else if (condition_2) {                     public class ifElseifExample {
                        // code if condition_2 is true               public static void main(String[] args) {
                  }                                           int marks = 65;
                  else if (condition_3) {
                        // code if condition_3 is true        if (marks <50) {
                  }                                                  System.out.println(‘Fail!”);
                  ..                                          }
                  else {                                      else if (marks >=50 && marks <60){
                        // code if all the conditions are false      System.out.println(“D Grade”);
                  }                                           }
                                                              else if (marks >=60 && marks <70){
                                                                     System.out.println(“C Grade”);
                          False
                Condition                                     }
                    1                                         else if (marks >=70 && marks <80){
                                                                     System.out.println(“B Grade”);
                           Condition  False                   }
                               2                              else if (marks >=80 && marks <90){

                True                                                 System.out.println(“A Grade”);
                           True                   False       }
                                     Condition
                 Statement               -n                   else if (marks >=90 && marks <100){
                      1               True                           System.out.println(“A+ Grade”);
                                                              }
                           Statement
                                2                             else {
                                                                     System.our.println(“Invalid!”);
                                     Statement                }
                                         n                    }                  Output:
                                                              }                  C Grade
                                               Statement
                                                    s

                                                                                                            27
   246   247   248   249   250   251   252   253   254   255   256