User Tools

Site Tools


guides:programstyle:goto

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
guides:programstyle:goto [2020/08/04 14:32] – created wikiadminguides:programstyle:goto [2022/08/02 11:59] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 All of these statements change the flow of control in a c++ program.  They change the behavior of control structures and therefore make it more difficult to understand the final flow of control in a program.  For these reasons, the use of these statements is heavily restricted. All of these statements change the flow of control in a c++ program.  They change the behavior of control structures and therefore make it more difficult to understand the final flow of control in a program.  For these reasons, the use of these statements is heavily restricted.
 +
 +===== Summary =====
 +
 +  * Do not use continue statements
 +  * Do not use goto statements
 +  * Only use break statements in switch statements
 +  * Only use a single return statement per function. It must be the last statement in the function.
 +  * All functions should have a return statement.
  
 ===== goto ===== ===== goto =====
  
 +The use of a goto statement transfers code to a predefined location in the program.  This can produce code which is difficult to follow and is therefore prohibited by the local style guide.
 ===== continue ===== ===== continue =====
  
 +The continue statement allows a programmer to skip the remaining statements in a control structure.  When employed in a loop, this will immediately transfer control to the end of the loop, however additional loop iterations may occur.
 +
 +The use of continue statements can cause surprising flow of control to those who are unaccustomed to their use.  
 +
 +The use of continue statements is not permitted in the local style guide. 
 ===== break ===== ===== break =====
  
 +The break statement causes control to be transferred to the end of the innermost control structure. It can be used to exit a loop without using the normal loop conditional statement.   Such a use will cause those unfamiliar with this programming style to misread the code. 
 +
 +Break is the primary method to exit a case or set of cases in a switch statement.
 +
 +The local style guide prohibits the use of **break** except in switch statements.
 ===== return ===== ===== return =====
 +
 +Return statements are used to transfer control from a called function to a calling function.  In C++ the return statement may or may not return a value, depending on the function prototype.
 +
 +Local convention states that each function should have exactly one return statement and that this statement should be the last line of code in the function.
 +
 +Examples  of acceptable use:
 +<code c++>
 +
 +// note the return for a void function has no argument.
 +void PrintHello(void) {
 +    cout << hello << endl;
 +    
 +    return;
 +}
 +
 +int Square(int n) {
 +    return n*n;
 +}
 +</code>
 +
 +A function should not have more than a single return.  The following example does not match the style guide:
 +<code c++>
 +
 +bool IsPositive(int n) {
 +    if (n > 0) {
 +       return true;
 +    } else {
 +       return false;
 +    }
 +}
 +</code>
 +
 +For more advanced classes, especially after the student has demonstrated a strong grasp of flow control and logical expressions, the above code **might** be acceptable.  In general, multiple returns should only be employed when the code is extremely simple.  Speciffically, replacing //break// statements in a switch structure with returns meets this criteria, as long as **all** break statements are replaced.
 +
 +With instructor approval, the both instances of the given code are acceptable.
 +
 +<code c++>
 +
 +string DigitName(int n) {
 +   string name;
 +     switch(n) {
 +      case 1: name = "One";
 +              break;
 +      case 2: name = "Two";
 +              break;
 +      case 3: name = "Three";
 +              break;
 +      ...
 +      
 +   
 +   return name;
 +}
 +
 +string DigitName(int n) {
 +   switch(n) {
 +      case 1: return "One";
 +      case 2: return "Two";
 +      case 3: return "Three";
 +      ...
 +      
 +   }
 +   return "";
 +}
 +
 +</code>
 +
 +Please consult your instructor before you use more than one return in a function.
 +
 +
guides/programstyle/goto.1596551531.txt.gz · Last modified: 2022/08/02 11:59 (external edit)