当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript用法:break,continue和return语句

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript用法:break,continue和return语句


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 457 ::
收藏到网摘: n/a

break,continue和return这三个语句的用法新手们经常弄混淆,至少在我学习C语言的时候经常把它们的用法给搞错。不过现在好了,我已彻底搞清楚它们之间的用法!!由于最近一直在看javascript,下面简要说一下它们三个在javascript的一些用法

break语句:
break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句。由于它是用来退出循环或者switch语句,所以只有当它出现在这些语句时,这种形式的break语句才是合法的。

如果一个循环的终止条件非常复杂,那么使用break语句来实现某些条件比用一个循环表达式来表达所有的条件容易得多。

<script type="text/javascript">
    for(var i=1;i<=10;i++){
        if(i==6) break;
        document.write(i);
    }
    //输出结果:12345
</script>


continue语句:
continue语句和break语句相似。所不同的是,它不是退出一个循环,而是开始循环的一次新迭代。

continue语句只能用在while语句、do/while语句、for语句、或者for/in语句的循环体内,在其它地方使用都会引起错误!


<script type="text/javascript">
    for(var i=1;i<=10;i++){
        if(i==6) continue;
        document.write(i);
    }
    //输出结果:1234578910
</script>


return语句:
return语句就是用于指定函数返回的值。return语句只能出现在函数体内,出现在代码中的其他任何地方都会造成语法错误!
当执行return语句时,即使函数主体中还有其他语句,函数执行也会停止!