break 在一些计算机程式语言中是保留字,其作用大多情况下是终止所在层的循环。在 C语言 的 switch(开关语句)中,break 语句还可用来在执行完一个 case(分支)后立即跳出当前 switch 结构。在某些程式调试过程中则使用break设定断点。
作用:设定或清除DOS系统的扩展ctrl+c检测(1)这个命令是为了与DOS系统的兼容而保留的,在Windows XP里不起作用;(2)如果命令扩展名被启用,并且操作平台是Windows XP,BREAK命令会在被调试程式调试时输入一个硬代码中断点。
break 结束当前 for,foreach,while,do-while 或者 switch 结构的执行。break 可以接受一个可选的数字参数来决定跳出几重循环。<?php$arr = array(‘one’, ‘two’, ‘three’, ‘four’, ‘stop’, ‘five’);while (list (, $val) = each($arr)) {if ($val == ‘stop’) {break; /* You could also write ‘break 1;’ here. */}echo “$val<br />/n”;}/* Using the optional argument. */$i = 0;while (++$i) {switch ($i) {case 5:echo “At 5<br />/n”;break 1; /* Exit only the switch. */case 10:echo “At 10; quitting<br />/n”;break 2; /* Exit the switch and the while. */default:break;}}?>
有两种特殊的语句可用在循环内部:break 和 continue。
break 命令可以终止循环的运行,然后继续执行循环之后的代码(如果循环之后有代码的话)。实例<html><body><script type=”text/javascript”>var i = 0for(i = 0; i <= 10; i++){ if (i == 3) { break }document.write(“The number is ” + i)document.write(“<br />”) }</script></body></html>结果The number is 0The number is 1The number is 2The number is 0The number is 1The number is 2
continue 命令会终止当前的循环,然后从下一个值继续运行。实例:<html><body><script type=”text/javascript”>var i = 0for(i = 0; i <= 10; i++) { if (i == 3) { continue } document.write(“The number is ” + i) document.write(“<br />”) } </script> </body> </html>结果:The number is 0The number is 1The number is 2The number is 4The number is 5The number is 6The number is 7The number is 8The number is 9The number is 10
break语句break语句通常用在循环语句和开关语句中。当break用于开关语句switch中时,可使程式跳出switch而执行switch以后的语句;如果没有break语句,则会从满足条件的地方(即与switch(表达式)括弧中表达式匹配的case)开始执行,直到switch结构结束。当break语句用于do-while、for、while循环语句中时,可使程式终止循环。而执行循环后面的语句,通常break语句总是与if语句联在一起。即满足条件时便跳出循环。例:
main(){ int i=0; char c; while(1) /*设定循环*/ { c='