JavaScript基础(一)

这两天刚做完项目,闲来无事 ,想好好了解了解JavaScript,于是决定写一些东西,把自己觉得细节的好好的总结起来,也算是一个学习的记录。

    今天要写的首先是JavaScript中Break 和 Continue语句的差别。

在Java中我们使用 break 语句来终止循环。 使用 continue 语句来终止当前的循环,然后从下一个值继续执行。 在JavaScript中也是一样 下面从两个实例可以很清楚的看出他们的区别。

实例一:使用Break语句。

<html>
   <body>
  <script type="text/javascript">
  var i=0
  for (i=0;i<=10;i++)
    {
      if (i==3) {break}
   document.write("数字值是:" + i)
   document.write("<br />")
   }
   </script>
  </body>
  </html> 运行后的结果是:数字值是:0                         数字值是:1                         数字值是:2

 

实例二:使用continue语句

                  <html>
                   <body>
                <script type="text/javascript">
                        var i=0
                        for (i=0;i<=10;i++)
                  {
                       if (i==3){continue}
                      document.write("The number is " + i)
                      document.write("<br />")
                      }
                      </script>
                      </body>
                     </html>

                    运行结果是:The   number is  0

                                      The   number is  1

                                      The   number is  2

                             The   number is  4                             The   number is  5                             The   number is  6                             The   number is  7                             The   number is  8                             The   number is  9                             The   number is  10

 

     由上面的是结果我们可以看出   

         break 命令是 终止循环的运行,然后继续执行循环之后的代码(如果循环之后有代码的话)。

       而continue 命令是终止当前的循环,然后从下一个值继续运行

 

  二、While循环和do  While循环的区别

 

       While循环是在指定条件为 true 时来循环执行代码。

       do...while 循环是 while 循环的变种。该循环程序在初次运行时会首先执行一遍其中的代码,然后当指定的条件为 true 时,它会继续这个循环。所以可以这么说,do...while 循环为执行至少一遍其中的代码,即使条件为 false,因为其中的代码执行后才会进行条件验证。

 

      多的话不说,详细的区别请看代码

    do    While循环的实例:<html>
                                                  <body>

                                                    <script type="text/javascript">
                                                                  i = 0
                                                                   do
                                                                     {
                                                                    document.write("数字是 " + i)
                                                                    document.write("<br>")
                                                                     i++
                                                                    }
                                                                    while (i <= 0)
                                                                    </script>

                         运行结果为:数字是0

 

 

           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值