switch case of application

  format:

Switch (n-) {
     Case A:
        // output statement 
       BREAK ;
     Case B:
        // output statement 
       BREAK ; 
   default: // output statement 
}

 

  The above n may be a number, arrays, strings.

  After when the n and a match, then the execution of the current case the following output statement, if break is present, name after the output immediately out of the current switch statement, if not break after the current case the output statement, the output current case statement continued execute judgment until the break out of the current switch.

  case can be many, may be added to break it is determined whether the execution end of the switch at an appropriate later case needed.

  In general, the default will be added in the switch which, if the value of the latter case is not equal to the variable, when executed default, and outputs the result.

  default is generally written in the switch Finally, as a conclusion, but do not need to break default statement.

  Example:

var i=3;
switch(i){
    case 1:
        console.log('一');
    case 2:
        console.log('二');
    case 3:
        console.log('三');
    case 4:
        console.lon('四');
    default:
        console.log('hello');
}

  The output is:

        three

        four

        hello

  Plus break:

var i=3;
switch(i){
    case 1:
        console.log('一');
    case 2:
        console.log('二');
    case 3:
        console.log('三');
    case 4:
        console.log('四');
        break;
    default:
        console.log('hello');
}

  The output is:

        three

        four

Guess you like

Origin www.cnblogs.com/zhangcheng001/p/10987833.html