Page 79 - Trackpad_ipro 4.1_Class8
P. 79

When you enter the value 7, then the output appears
                 as shown.

                 Because the condition (7% 2 == 0) in the 'if' statement
                 returns false, control is transferred to the statements
                 in the 'else' block.
                 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 '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]
                 }                                                  Conditional      False
                                                                    Expression
                 else if(< conditional expression >)
                                                                        of if
                 {

                        [statements]                               True               Conditional      False
                                                                                      Expression
                 }
                                                                     Body of if        of else if
                 ……

                 else                                                                       True
                 {
                                                                                    Body of else if    Body of else
                        [statements]

                 }
                 For example:                                          Stop
                 public class IfElseIfStatement
                 {
                        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')
                               {



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