User Tools

Site Tools


guides:programstyle:goto

Use of goto/break/continue/return Statements

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

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

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

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 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:

// note the return for a void function has no argument.
void PrintHello(void) {
    cout << hello << endl;
 
    return;
}
 
int Square(int n) {
    return n*n;
}

A function should not have more than a single return. The following example does not match the style guide:

bool IsPositive(int n) {
    if (n > 0) {
       return true;
    } else {
       return false;
    }
}

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.

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 "";
}

Please consult your instructor before you use more than one return in a function.

guides/programstyle/goto.txt · Last modified: 2022/08/02 11:59 by 127.0.0.1