JavaScript Switch

Analyzing the condition of the if statement is generally used for comparison, appropriate determination condition is a Boolean value.

If the determination condition expression value is a character, or digital, and has more than two values, the better use of the switch statement.

switch statement syntax is as follows:

switch (condition) {
  Case value1:
    // If value1 when the matching condition results, performed here for the statement
    [BREAK;]
  Case value2:
    // the conditions when the matching results value2, execute the statement where
    [BREAK;]
  . ..
  Case valueN:
    // when the result of the matching condition with valueN, execute the statement where
    [BREAK;]
  [default:
    // if the condition does not match the value of the above value, the statement executed here
    [BREAK;]]
}

Example:

. 1  var Score = " . 1 " ;
 2  Switch (Score) {
 . 3  Case  " . 1 " :
 . 4     document.write ( " you are number. 1 " );
 . 5     BREAK ;
 . 6  Case  " 2 " :
 . 7      document.write ( " you are No. 2 " );
 . 8      BREAK ;
 . 9  Case  " 3 " :
 10      document.write ( " you are 3 " );
11      BREAK ;
 12  Case  " 4 " :
 13      document.write ( " You're No. 4 " );
 14      BREAK ;
 15  default :
 16      document.write ( " Unfortunately, you do not have numbers " );
 17      BREAK ;
 18 }

 

Guess you like

Origin www.cnblogs.com/q2546/p/11025332.html