zoukankan      html  css  js  c++  java
  • break,continue,goto,Return几个方法

     

    上次码代码的时候发现不是很清楚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或则全部执行完语句在跳出函数

  • 相关阅读:
    【转】Math.Atan2 方法
    【转】Excel快捷键大全
    Tricks(四十七)—— 布尔矩阵(0-1矩阵)取反
    Tricks(四十七)—— 布尔矩阵(0-1矩阵)取反
    tensorflow 函数接口的理解
    tensorflow 函数接口的理解
    TensorFlow 学习(十一)—— 正则(regularizer)
    TensorFlow 学习(十一)—— 正则(regularizer)
    TensorFlow 需注意的细节问题
    TensorFlow 需注意的细节问题
  • 原文地址:https://www.cnblogs.com/kounian/p/3381320.html
Copyright © 2011-2022 走看看