Page 80 - TP_iPlus_V2.1_Class8
P. 80

The if...else...if Statement

                  The if…else…if statement helps us to test multiple conditions and execute one of the multiple
                  blocks of statements. These blocks are specified by if and else…if keywords. The else block will
                  be executed if any of the given conditions in the if and the else…if blocks does not evaluate to
                  true. The if…else…if statement is also known as the if…else ladder.
                  The syntax of the if...else…if statement is as follows:

                  if (< conditional expression >)                           Start
                  {
                         [statements]
                  }
                  else if(< conditional expression >)                   Conditional     False
                                                                         Expression
                  {
                                                                            of if
                         [statements]
                  }                                                                       Conditional
                                                                              True                       False
                  ……                                                                       Expression
                                                                                           of else if
                  else                                                   Body of if
                  {
                                                                                                True
                         [statements]
                  }                                                                      Body of else if  Body of else
                  For example:
                                                                           Stop
                  public class IfElseIfStatement
                  {                                                         Flowchart of if....else....if statement
                         public static void main(char ch)
                         {
                                char C = ch;
                                System.out.println("Entered character is: " + ch);
                                if(C == 'W')
                                {
                                       System.out.println("Enjoy Winter");
                                }
                                else if(C == 'R')
                                {
                                       System.out.println("Enjoy Rainy");
                                }
                                else if (C == 'S')
                                {
                                       System.out.println("Enjoy Summer");
                                }
                                else
                                {
                                       System.out.println("Invalid Character");
                                }
                         }
                  }



                    78
                         iPlus (Ver. 2.1)-VIII
   75   76   77   78   79   80   81   82   83   84   85