独闷闷网

 找回密码
 立即注册
搜索
查看: 3773|回复: 1
收起左侧

[分享] break,return,contiune...讲解

[复制链接]
发表于 2015-3-25 11:44:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
讲解的很全面,与诸位共享。
一.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或则全部执行完语句在跳出函数

乐于分享,勇于质疑!
发表于 2015-3-25 22:19:07 | 显示全部楼层
感谢分享。
乐于分享,勇于质疑!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|独闷闷网 ( 粤ICP备12007667号-2 )

GMT+8, 2024-3-29 22:45 , Processed in 0.163562 second(s), 17 queries .

快速回复 返回顶部 返回列表