Page 77 - iPlus_Ver_2.0_class_8
P. 77

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
                 else                                                   Body of if        of else 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");
                               }
                        }
                 }



                                                                                                                  75
                                                              Conditional, Looping and Jumping Statements in Java
   72   73   74   75   76   77   78   79   80   81   82