guides:programstyle:goto
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
guides:programstyle:goto [2020/08/04 14:32] – created wikiadmin | guides:programstyle:goto [2024/07/25 15:01] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
All of these statements change the flow of control in a c++ program. | All of these statements change the flow of control in a c++ program. | ||
+ | |||
+ | ===== 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. | ||
===== continue ===== | ===== continue ===== | ||
+ | The continue statement allows a programmer to skip the remaining statements in a control structure. | ||
+ | |||
+ | 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. | ||
+ | |||
+ | 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. | ||
+ | |||
+ | 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 | ||
+ | <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; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | A function should not have more than a single return. | ||
+ | <code c++> | ||
+ | |||
+ | bool IsPositive(int n) { | ||
+ | if (n > 0) { | ||
+ | | ||
+ | } else { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | For more advanced classes, especially after the student has demonstrated a strong grasp of flow control and logical expressions, | ||
+ | |||
+ | With instructor approval, the both instances of the given code are acceptable. | ||
+ | |||
+ | <code c++> | ||
+ | |||
+ | string DigitName(int n) { | ||
+ | | ||
+ | | ||
+ | case 1: name = " | ||
+ | break; | ||
+ | case 2: name = " | ||
+ | break; | ||
+ | case 3: name = " | ||
+ | break; | ||
+ | ... | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | string DigitName(int n) { | ||
+ | | ||
+ | case 1: return " | ||
+ | case 2: return " | ||
+ | case 3: return " | ||
+ | ... | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | Please consult your instructor before you use more than one return in a function. | ||
+ | |||
+ |
guides/programstyle/goto.1596551531.txt.gz · Last modified: 2024/07/25 15:01 (external edit)