点击(此处)折叠或打开

  1. window.onload=function(){
  2.     for (var i = 1; i <=5; i++) {
  3.         console.log(i*i+'for');
  4.     };
  5.     console.log(i*i+"for-out");
  6.     whiletest();
  7. }
  8. function whiletest(){
  9.     var i =1;
  10.     while(i<=5){
  11.         console.log(i*i+'while');
  12.         i++;
  13.     }
  14.     console.log(i*i+"while-out");
  15. }
控制台结果:

点击(此处)折叠或打开

  1. 1for
  2. 4for
  3. 9for
  4. 16for
  5. 25for
  6. 36for-out
  7. 1while
  8. 4while
  9. 9while
  10. 16while
  11. 25while
  12. 36while-out
之前一直没有注意,也怪自己学艺不精,i变量循环时,在满足循环条件后,还会再次累加一次,以便判断是否满足循环条件,这是的i已经变成6了。不满足跳出循环,可是i=6已经是事实!!
09-09 03:53