if statement and switch statement

Conditional statements

  if (condition) {

    Code execution if the condition is true

  }

  Example:

    There are a = 1;

    if (a < 3) {

      document.write('hell word!');

    }

 

  if (condition) {

    Code execution if the condition is true

  }else{

    When the code is executed if the condition is false

  }

  Example:

    There are a = 1;

    if (a < 3) {

      document.write('hello');

    }else{

      document.write('word');

    }

   The results: hello

 

  if (condition 1) {

    Condition 1 block of code is true

  } Else if (condition 2) {

    Condition 2 is true execution code block

  }else{

    Conditions 1 and 2 are not simultaneously satisfied during execution of the code block

  }

   Example:

      There are a = 87;

        if (a >= 90) {

          document.write ( 'excellent');

        }else if(a>60 &&  a<90){

          document.write('良好');

        }else{

          document.write ( 'fail');

        }

      Results: Good

switch statement

  grammar

    switch (expression) {

      case n:

        Block

        break;                      

      case n:

        Block

        break;

      default:

        The default block 

    }

  Example 1:

    switch (2){

      case 1: 

        document.write('aa');
        break;

      case 2:
        document.write('bb');
        break;

      case 4:
        document.write('dd');
        break;

      default:

        document.write('ff');

    }

     The output is: bb

  Example 2:

    switch (true){

      case 1: 

        document.write('aa');
        break;

      case 2:
        document.write('bb');
        break;

      case true:
        document.write('dd');
        break;

    }

    The result is: dd

  By Examples 1 and 2 can be found, it can not only output a digital input Boolean may also be described that the data transmission switch

  Example 3: 

    i=14;
    switch(i){
      case 1:
        document.write('a');
      case 2:
        document.write('b');
      default:
        document.write('e');
      case 3:
        document.write('c');
        break;
      case 4:
      document.write('d');

    }

  The results: ec

  break; it will jump out of the switch code.

  The last code block Case, here will naturally block the end, it does not require additional break ;.

  Default default can not be placed in the last case.

 

 

 

 

 

    

 

 

 

 

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/hebizaiyi/p/11346345.html