首页 > 代码库 > break,continue,return的区别
break,continue,return的区别
1)break
作用:跳过当前一个循环体
eg1:
<pre name="code" class="php"><pre name="code" class="php" style="font-size: 12px;"><?php <span style="color:#330033;">for</span>($a=1;$a<10;$a++){ echo '======<br/>'; <span style="background-color: rgb(0, 0, 153);"><span style="color:#ffff00;"> for($b=$a;$b<10;$b++){ if($b==5){ break; echo 'hi'; }else{ echo $a.'*'.$b.'<br/>'; } }</span></span>
//如果此行有代码,时break生效时,执行权从黄色的for循环体直接跳到此行. } ?>
执行结果:
<span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*1</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*2</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*3</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*2</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*3</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*3</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">4*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*6</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">7*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">7*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">7*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">8*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">8*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">9*9</span>说明,break的作用是把蓝块代码删除(忽略/打断)了一次.执行权再跳到注释的代码行.
2)continue
跳过当前一个循环体中的一次循环
eg2:
<?php for($a=1;$a<10;$a++){ echo '======<br/>'; <span style="color:#ff0000;">for</span>($b=$a;$b<10;$b++){ if($b==5){ <span style="color:#ffff00;background-color: rgb(0, 0, 153);"> continue; echo 'hi';</span> }else{ echo $a.'*'.$b.'<br/>'; } } } ?>
执行结果:
<span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*1</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*2</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*3</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*6</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">1*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*2</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*3</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*6</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">2*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*3</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*6</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">3*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">4*4</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">4*6</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">4*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">4*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">4*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">5*6</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">5*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">5*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">5*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*6</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">6*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">7*7</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">7*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">7*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">8*8</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">8*9</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">======</span><br style="font-family: Times;" /><span style="font-family: Times;font-size:14px;">9*9</span>说明:continue的作用是将 echo ‘hi‘; 代码删除(忽略)一次,执行权再跳到红色的for.
3)reutrn
中断函数,并返回一个值(也可能是NULL)
break,continue,return的区别
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。