Home>

The switch statement is used to perform different actions based on different conditions.

switch (expression) {
   case n:
    Code block
    break;
   case n:
    Code block
    break;
   default:
    Default code block
}

Code explanation:

Evaluate a switch expression once Compare the value of the expression to the value of each case If there is a match,Then execute the associated code

as follows:

switch (new date (). getday ()) {
  case 0:
    day="Sunday";
    break;
  case 1:
    day="Monday";
     break;
  case 2:
    day="Tuesday";
     break;
  case 3:
    day="Wednesday";
     break;
  case 4:
    day="Thursday";
     break;
  case 5:
    day="Friday";
     break;
  case 6:
    day="Saturday";
}
  • Previous Implementation of Django routing layer URLconf
  • Next 4 examples and principles of Python crawler parsing web pages