Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 104 - 2617_JSSPS_C-7
P. 104

    5          char C = ch;

                  6               System.out.println("Entered character is: " + ch);
                  7          if(C == 'W')

                  8          {

                  9              System.out.println("Enjoy Winter");

                  10         }

                  11         else if(C == 'R')

                  12         {
                  13             System.out.println("Enjoy Rainy");

                  14         }

                  15         else if(C == 'S')
                  16         {

                  17             System.out.println("Enjoy Summer");

                  18         }
                  19         else

                  20         {

                  21             System.out.println("Invalid Character");

                  22         }
                  23      }

                  24  }
                  Run this program four times with the inputs 'W', 'R', 'S' and 'T', respectively. You will see the output as
                  “Enjoy Winter”, “Enjoy Rainy”, “Enjoy Summer” and “Invalid Character” respectively. This is because each
                  time the condition is checked with the conditional expression written after the if and else…if keywords,
                  it executes the statements written inside the block where a perfect match is found.

                            Premium Edition-VII
                    102

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