Javascript 第四天

 

一、循环语句

1、while循环

<script type="text/javascript">

var i=1;

           while(i<=1000){

           if(i%2==1){

                 document.write(i+"<br/>")

           }

           i++;

      }

</script>

 

2、do while循环

<script type="text/javascript">

      var i=1;

      do{

           document.write("<span>"+i+"</span>"+"<br/>");

           i++;

      }while(i<=1000);

 

      var i=1;

      do{

           if(i%2==0){

           document.write("<span>"+i+"</span>"+"<br/>");

           }

           i++;

      }while(i<=1000);

 

 

      var i=1;

      do{

           if(i%2==1){

           document.write("<span>"+i+"</span>"+"<br/>");

           }

           i++;

      }while(i<=1000);

 

 

      </script>

     

      <style type="text/css">

           span{

                 text-decoration: underline;

           }

      </style>

3、for循环

<script type="text/javascript">

           for(var i=1;i<10;i++){

                 document.write('<img src="images/'+i+'.png">')

           }

      </script>

4、 break 语句和 continue 语句

      break 语句可以立即退出循环;

      continue 语句只是退出当前循环;

例:     var n = 0;

      for(var i = 1; i < 10; i++){

           if(i == 4){

               continue;

           }

           n++;

      }

      alert(n);

例:

      var n = 0;

      for(var i=1; i < 10; i++){

           if( i > 5){

              break;

           }

           n++;

      }

      alert(n);

二、函数

基本语法:

      function funName (arg0, arg1, … argN){

           //statements

      }

例1:

      function say_hello (name, msg){

           alert(“hello”+ name + “:”+ msg);

      }

      say_hello(“david”, “how are you today?”);

例2:使用 return 语句从函数返回值

      function sum (num1, num2){

           return num1 + num2;

      }

      var s = sum(1, 2);

      alert(s);

 

posted @ 2016-09-05 22:16  蔚201388  阅读(145)  评论(0编辑  收藏  举报