Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 103 - 2617_JSSPS_C-7
P. 103

    When you enter the value 7, then the output will appear 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 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:
                                                                                  Start
                     if (< conditional expression >)
                     {

                         [statements]
                                                                              Conditional     False
                     }                                                         Expression
                                                                                  of if
                     else if(< conditional expression >)
                     {                                                                          Conditional     False
                                                                                    True
                                                                                                Expression
                       [statements]
                                                                               Body of if        of else if
                     }

                     ……                                                                               True

                     else                                                                     Body of else if   Body of else
                     {
                                                                                 Stop
                         [statements]
                                                                                  Flowchart of if....else....if statement
                     }

                      Program 7:  Write a program to check the season based on the character entered by the user.

                     1   public class IfElseIfStatement

                     2   {
                     3       public static void main(char ch)

                     4       {

                                                                                                   Java Programming    101

| 98 99 100 101 102 103 104 105 106 107 108 |