break,continue,goto,return语句区别详解

一。break

    1. break语句形式: break;
    2. break语句功能:
A. switch语句中,break语句会终止其后语句的执行,退出switch语句。
B. 使一个循环立即结束,也就是说在循环中遇到break语句时,循环立即终止,
程序转到循环体后的第一个语句去继续执行。
    3. 注:
A. break语句在循环中使用时,总是与if一起使用,当条件满足(或不满足)
时,负责退出循环。
       B. 如果循环体中使用switch语句,而break出现在switch语句中,则它只用
于结束switch,而不影响循环。
       C. break语句只能结束包含它的最内层循环,而不能跳了多重循环。
    4. 例: break语句的执行只能从while循环中退出,继续执行for循环的其它语句
而不是退出外层循环。
       for()

       {

           :

           :

           while()

           {

              :

              :

              if() break;

              :

              :

           }

           :

           :

       }

 

 

二。continue   

    1.continue语句形式: continue;
    2.continue语句功能:   它只能出现在循环体中,其功能是立即结束本次循环,
即遇到continue语句时,不执行循环体中continue后的语句,立即转去判断循环条件是否成立。  

3.Continue与break语句的区别: continue只是结束本次循环,而不是终止整个循
环语句的执行,break则是终止整个循环语句的
执行,转到循环语句后的下一条语句去执行。
     程序表达式及流程图如下:
(1.)        while(表达式1)                  (2.)while(表达式1)

{                                    {

      :                                   :

           If(表达式2) break;                  if(表达式2) continue;

           :                                    :

       }                                    }

 

 

三.goto

1.goto语句形式:goto语句是无条件转向语句,其一般形式为:
                 goto 语句标号;

    2.功能:goto语句往往用来从多重循环中跳出。它在解决一些特定问题时很方便,但由于goto语句难于控制,尽量少用。
    3.例:
           IN:

              For()

              {

                   :

                   :

                   Goto IN;

               }

             

   

四.Return

   

1.return语句形式: return (返回值);

    2.功能:return可以返回一个函数的值,并且跳出这个函数;

 

      Void doSomething(){

       do whatever is required by this method …

       return;

    }
 
只要遇到return语句,程序就在那一行代码停止执行,执行控制将立刻返回到调用该程序的代码处。
对于返回值类型为void的程序,return关键字单独作为完整的语句使用:
return;
 
1.    对于返回类型为void的程序,return;语句是可选的。如果省略这条语句,隐含表明程序的最后一行有一个return;语句。即,下面两个版本的doSomething程序是等价的:
 
void doSomething(){

     int x=3;

     int y=4;

     int x=x+y;

}


void doSomething(){

     int x=3;

     int y=4;

     int x=x+y;

     return;

}

 

2.    对于返回类型非void的程序体,必须至少包括一条明确的return语句。这种情况下,return关键字后面必须跟随一个求值类型和程序声明的返回类型一致的表达式。例如,如果程序定义为具有int返回类型,那么下列任何一种return语句都可以接受:
 
return 0; //returning a constant integer value

return x; //returning the value of x(assuming that x has previously been declared to be an int)

return x+y; //returning the value of the expression”x+y”(here,we`re assuming that “x+y” evaluates to an int value)

return (int)z; //casting the value of z(assume z was declared as a double to an int value)
 
3.    如果程序定义为具有boolean返回类型,那么下列任何一种return语句都可以接受:
 
return false;   //returning a Boolean constant value

return outcome; //returning the value of variable outcome (assuming that outcome has previously been declared to be of type Boolean)

return(x<3);    //returning the Boolean value that results when the value of x is compared to 3: if x if less than 3, this method returns a value of true; otherwise, it returns false.
 
程序体可以包含不只一条return语句。但好的编程习惯是一个程序中只在最末尾包含一条return语句。再看一下前面讨论过的isHornorsStudent程序,这个程序有两条return语句:
boolean isHonorsStudent(){

if(gpa>=3.5) return true; //first return statement

else return false; //second return statement

}

 

使用局部boolean型变量result来重写这个程序,该变量捕获最终返回的true/false结果。在程序最末尾用一条return语句返回result变量的值:
boolean isHonorsStudent(){

boolean result = false;

if(gpa>=3.5) result = true;

else result = false;

return result;

}

如上述代码所示,由于已经给result变量赋予初值false,因此在else子句中明确地给它赋值false是不必要的,可以像下面这样简化isHornorsStudent程序:
boolean isHonorsStudent(){

boolean result = false;

if(gpa>=3.5) result = true;

return result;

}
 
但是有一种情况下多个return语句是可以接受的:程序需要执行一系列操作,在这个过程中,任何一步失败意味着整个程序失败。下面的伪代码阐明了这种情况:
 
function cs(n){

if(n==1) { retrun 1; }

if(n==2) { return; }

else { }

}

说明:   当参数为1时函数返回值为1并且跳出函数,

                当参数为2时函数返回值为空并且跳出函数,

                当函数为其他值时继续执行函数下面的语句,直到遇到下个return或则全部执行完语句在跳出函数

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wai631/archive/2009/07/24/4376219.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值