User Tools

Site Tools


guides:programstyle:goto

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guides:programstyle:goto [2020/08/04 14:46] – [return] 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 =====
  
Line 42: Line 61:
 </code> </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.+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. With instructor approval, the both instances of the given code are acceptable.
guides/programstyle/goto.1596552398.txt.gz · Last modified: 2022/08/02 11:59 (external edit)