Page 81 - iPro_trackGPT_V5_Class8
P. 81

The if...else...if Statement

                 The if…else…if statement helps test several conditions and run one of the different blocks
                 of  code.  The  blocks  of  code  are  specified  using  the  if  and  else…if  keywords.  If  none  of  the
                 conditions in the if and else…if blocks are true, the code inside the else block will run. This
                 type of statement is also called an 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
                        [statements]                                       of if
                 }
                 ……                                                          True        Conditional    False
                                                                                         Expression
                 else
                                                                        Body of if        of else if
                 {
                        [statements]
                                                                                              True
                 }

                 For example,                                                          Body of else if   Body of else
                 public class IfElseIfStatement
                                                                          Stop
                 {
                        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");
                               }
                        }
                 }





                                                                Conditional, Looping and Jump Statements in Java  79
   76   77   78   79   80   81   82   83   84   85   86