break 与 continue

break and continue difference

 break statement: interrupt the cycle, and continue to execute code after the loop

  Example:

    for (var i = 1; i> = 0; ++ i {

      if(i>10){

        break;

      }

    document.write(i+' ');

    }

     Results: 12345678910

  continue statement: On behalf jump out when the cycle into the next cycle

  Example 1:

 

    for(var i=1;i<=10;i++){
      if(i==3){
        document.write('hello_king');
        continue;
      }
    document.write(i+'&nbsp');
    }

      Results: 1 2 hello_king 4 5 6 7 8 9 10

  Example 2:   

    i=1;
      while(i<=10){
        if(i==3){
          continue;
          i++;
        }
        document.write(i+'&nbsp');
        i++;
      }

    Results: infinite loop

   Reason: when i == 3, out of the while loop, at which point i is equal to 3, while loop enters again into the if condition i is equal to 3, out of the while loop again, behind the same token, the value of i is always 3 , so the results of the cycle is dead.

    Amendment:

      i=1;
      while(i<=10){
        if(i==3){

          i++;

          continue;
      
  }
        document.write(i+'&nbsp');
        i++;
      }

    I ++ to the front will continue, when i == 3, into the condition if, for the first + 1, i is now 4, Continue out of the loop proceeds while loop again, the value of i is 4, does not enter if condition, so the result is 1,245,678,910.

 

Guess you like

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