Presentation is loading. Please wait.

Presentation is loading. Please wait.

Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in Programming Issues with Return statements Recursion and.

Similar presentations


Presentation on theme: "Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in Programming Issues with Return statements Recursion and."— Presentation transcript:

1 Statements and Control Issues By Chris Bradney

2 Overview of topics Typical Control Statements in Programming Issues with Return statements Recursion and its uses goto statements: arguments for and against, and proper use of them

3 Types of Control Structures in Coding Languages Sequential Sequential Statements Operations performed in order Selection Statements Checks on conditions for code execution Iteration Statements Series of Statements to be performed until a condition is met

4 Types of Control Structures in Coding Languages Selection Sequential Statements Operations performed in order Selection Statements Checks on conditions for code execution Iteration Statements Series of Statements to be performed until a condition is met

5 Types of Control Structures in Coding Languages Iteration Sequential Statements Operations performed in order Selection Statements Checks on conditions for code execution Iteration Statements Series of Statements to be performed until a condition is met

6 Multiple Return Statements A function can have multiple statements that can cause the program to exit. Int Comparison (int a, int b){ if(a

7 Multiple Return Statements – Readability Multiple return statements can cause a program to be more readable than an alternative with nested if statements. If(isValid(name)) if(isNew(name)) insert(name); else errorOut(false);

8 Multiple Return Statements – Guardian cases Using multiple return statements in a function can act as guardian statements to qualify the standard case. If(isValid(name)!){ errorFlag = true; return; } Else //code

9 Recursion Recursion can be a powerful tool to solve complex problems in elegant ways. Most problems that can be resolved with recursion can be resolved more typical ways.

10 Recursion – Important Considerations Recursion programmers need to assure an end to the cycle Recursion needs to be controlled in the number of iterations it performs before an error is thrown Recursion programmers need to prevent cyclical recursive calls Recursion should not be used in trivial cases

11 Recursion – example case Void Recur (int safetyCounter){ if (safetyCounter > MaxLimit){ //error case else //code Recur (safetyCounter+1); }

12 goto Statements goto statements allow a program to jump to a specified location elsewhere in code The location of the jump is not restricted There is a heated debate on proper practices of use of goto statements even today

13

14 goto statements – arguments against In March of 1968, Edsger Dijkstra penned a famous letter in the Communications of the ACM stating that the overall quality of code was inversely proportional to the number of goto statements used. goto statements make reading code more difficult goto statements make formatting code more difficult goto statements works against the compiler goto statements can make code slower

15 goto statements – arguments for goto statements can eliminate the need for duplicate code sections goto statements have code blocks that allocate, use, and deallocate memory goto statements can lead to smaller, faster code evidence has shown that the use of goto statements is not directly related to poor quality code goto statements are included in many modern languages

16 goto statement uses goto statements can be used to provide dynamic error checking features without the need for nested if statements if (openFile(fopen)!) error = fileOpen; GOTO END_PROC if (overwrite(fopen)!) error = fileOverwrite; GOTO END_PROC END_PROC: //Code if (openFile(fopen)) if (overwrite(fopen)) //Code else error = fileOverwrite; else error = fileOpen;

17 goto statement – replace with status variables One possible replacement for a goto statement is a status variable check as if statements if(errorState == success) if(openFile(fopen)!) errorState = fileOpen; if(errorState == success) if(overwriteFile(fopen)!) errorState = fileOverwrite;

18 goto statement substitutions Each possible replacement for the use of goto statements has its own pros and cons goto statements can be successfully used for the benefit of the program when used right, and should not always be replaced It is sometimes easier to write a routine with goto statements than without, and goto statements can make it easier to understand what is happening in the code

19 Final goto statement considerations goto statements can be replaced in many instances, and is good practice for programmers to goto statements should only be used when the circumstances dictate the necessity Try to limit goto statements to jumping forward Understand not all goto statements are bad

20 Summary Multiple return statements: can aid error checking and readability harder to understand if too complex Recursion: powerful tool to conquer problems dangerous or foolish when misused goto statements: available tool for programmers to use not recommended for casual use

21 The End goto end_of_presentation


Download ppt "Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in Programming Issues with Return statements Recursion and."

Similar presentations


Ads by Google