Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in...

21
Statements and Control Issues By Chris Bradney

Transcript of Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in...

Statements andControl Issues

By Chris Bradney

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

Types of Control Structuresin 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

Types of Control Structuresin 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

Types of Control Structuresin 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

Multiple Return Statements

A function can have multiple statements that can cause the program to exit.

Int Comparison (int a, int b){

if(a<b) return a;

else return b;

}

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);

else errorOut(false);

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

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.

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

Recursion – example case

Void Recur (int safetyCounter){

if (safetyCounter > MaxLimit){

//error case

else

//code

Recur (safetyCounter+1);

}

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

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

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

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;

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;

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

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

Summary

Multiple return statements:can aid error checking and readabilityharder to understand if too complex

Recursion:powerful tool to conquer problemsdangerous or foolish when misused

goto statements:available tool for programmers to usenot recommended for casual use

The Endgoto end_of_presentation