Page 130 - CA_Blue( J )_Class10
P. 130

16                          break;
                17              case 2:     p = 4 * s;

                18                          System.out.println("Perimeter : "+p);
                19                          break;

                20              default:    System.out.println("Wrong Choice ");
                21          }

                22      }
                23  }

              You will get the following output:





















                   7.5 FALL-THROUGH SITUATION
              In Java, the break statement is used to terminate the execution of a loop or a switch-case structure. In a switch
              statement, the break statement is used to exit the switch block once a matching case is found and its associated code
              is executed. Without the break, the control will "fall through" to subsequent cases, executing their code as well, even
              if they don't match the value of the switch expression. Let us take an example:
                  int day = 2;
                  switch (day) {
                      case 1:
                          System.out.println("Sunday");
                          break;
                      case 2:
                          System.out.println("Monday");
                          break;
                      case 3:
                          System.out.println("Tuesday");
                          break;
                      default:
                          System.out.println("Invalid day");
                          break;
                  }
              If the case value does not match with the choice, then the default statement is executed. Thus, a default is executed
              only if no case is matching the value of the day variable.






                128128  Touchpad Computer Applications-X
   125   126   127   128   129   130   131   132   133   134   135