Page 182 - Computer science 868 Class 12
P. 182

if else if Statement
              The if else if statement has multiple if statements or conditions, like the if and only if statements discussed above,
              where conditions are checked one by one. But with a difference that in this statement, if the first condition is satisfied,
              then it will execute the respective statements and will not check the remaining conditions.
              Syntax:

                  if(condition 1)
                  {
                      Statement 1;
                  }
                  else if(condition 2)
                  {
                      Statement 2;
                  }
                  else if(condition 3)
                  {
                      Statement 3;
                  }
                  else
                  {
                      Statement last;
                  }
              For example:
              Input marks in Physics, Chemistry and Maths. Print whether the student has got Science, Commerce or Arts stream
              based on the following criterion:

              a.  if marks in Physics, Chemistry and Maths > 80 – Science stream
              b.  if marks in Physics and Chemistry < 80 and marks of Maths > 80 – Commerce stream
              c.  if marks in Physics, Chemistry and Maths < 80 – Arts stream

                  class StreamSelected
                  {
                      public static void main(int pm, int cm, int mm)
                      {
                          if(pm>=80 && cm>=80 && mm>=80)
                          {
                              System.out.println("Selected for Science");
                          }
                          else
                          if pm<80 && cm<80 && mm>=80)
                          {
                              System.out.println("Selected for Commerce");
                          }
                          else
                          if(pm<80 && cm<80 && mm<80)
                          {
                              System.out.println("Selected for Arts");
                          }
                      }
                  }
              Input:
              Physics = 90
              Chemistry = 99
              Maths = 98


                180180  Touchpad Computer Science-XII
   177   178   179   180   181   182   183   184   185   186   187