17. Unusual Control Structures

Slides:



Advertisements
Similar presentations
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Advertisements

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Recursion CS 367 – Introduction to Data Structures.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Statements and Control Issues Ch 17, 19 Michael Murphy CS 525 Software Engineering II Dr Xianzhong Liang.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in Programming Issues with Return statements Recursion and.
CS0004: Introduction to Programming Repetition – Do Loops.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Computer Science II Recursion Professor: Evan Korth New York University.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
CS 240: Data Structures Tuesday, June 5 th Programming Semantics, Software Life Cycle.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
CS 211 RECURSION TREES. Trees versus Linked Lists A tree is like a linked list, except instead of a single next node, it can have multiple next nodes.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 4 Program Control Statements
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
 Thursday: › Team Presentations › Risk Assessment and project plan due 11:55 pm  Friday: › Help on coding/testing  Monday: › HW 5 due, 11:55 pm.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Looping and Counting Lecture 3 Hartmut Kaiser
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion Powerful Tool
CS314 – Section 5 Recitation 9
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Fundamentals of PL/SQL part 2 (Basics)
Introduction to Recursion
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
The Pseudocode Programming Process
Java Software Structures: John Lewis & Joseph Chase
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Recursion 12-Nov-18.
Lesson #6 Modular Programming and Functions.
Recursion Output Input
Topics Introduction to File Input and Output
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Chapter 6 Decision Making and Looping
During the last lecture we had a discussion on Data Types, Variables & Operators
Recursion 29-Dec-18.
Homework Any Questions?.
Basics of Recursion Programming with Recursion
Lesson #6 Modular Programming and Functions.
Yan Shi CS/SE 2630 Lecture Notes
Recursion 23-Apr-19.
Introduction to Programming
Topics Introduction to File Input and Output
Controlling Program Flow
Presentation transcript:

17. Unusual Control Structures CSC-3004 Introduction to Software Development Spring 2015 Dr. James Skon

Topics Multiple Returns from a Routine Recursion goto Perspective on Unusual Control Structures

Multiple Returns from a Routine The return and exit statements are control constructs that enable a program to exit from a routine at will. Use a return when it enhances readability. In certain routines, once you know the answer, you want to return it to the calling routine immediately.

Example of a Good Multiple Return from a Routine Comparison Compare( int value1, int value2 ) { if ( value1 < value2 ) { return Comparison_LessThan; } else if ( value1 > value2 ) { return Comparison_GreaterThan; return Comparison_Equal; returns a Comparison enumerated type

Code that obscures Nominal Case If file.validName() { If file.Open() { If encryptionKey.valid() { If file.Decrypt( encryptionKey ) { // lots of code ... } CODE FOR NOMINAL CASE

Code That Uses Guard Clauses to Clarify the Nominal Case int doEncryption(…) { // set up, bailing out if errors are found if (!file.validName()) return; if (!file.Open()) return; if (!encryptionKey.valid()) return; if (!file.Decrypt( encryptionKey )) return; // lots of code … return success; }

Recursion In recursion, a routine solves a small part of a problem itself, divides the problem into smaller pieces, and then calls itself to solve each of the smaller pieces.

Recursion

Recursion void QuickSort( int firstIndex, int lastIndex, String [] names ) { if ( lastIndex > firstIndex ) { int midPoint = Partition( firstIndex, lastIndex, names ); QuickSort( firstIndex, midPoint-1, names ); QuickSort( midPoint+1, lastIndex, names ) } }

EXAMPLE OF RECURSION Trying to find path through a maze Recursive Solution: Try every path: Left, Up or Down, Right As you visit each place, drop a bread crumb to you know you were there, and don’t try it again.

MAZE bool FindPathThroughMaze( Maze maze, Point position ) { // if the position has already been tried, don't try it again if ( AlreadyTried( maze, position ) ) { return false; } // if this position is the exit, declare success if ( ThisIsTheExit( maze, position ) ) { return true; // remember that this position has been tried RememberPosition( maze, position ); // check the paths to the left, up, down, and to the right; if // any path is successful, stop looking if ( MoveLeft( maze, position, &newPosition ) ) { if ( FindPathThroughMaze( maze, newPosition ) ) {

MAZE if ( MoveUp( maze, position, &newPosition ) ) { if ( FindPathThroughMaze( maze, newPosition ) ) { return true; } if ( MoveDown( maze, position, &newPosition ) ) { if ( MoveRight( maze, position, &newPosition ) ) { return false;

TIPS FOR USING RECURSION Make sure the recursion stops. Use safety counters to prevent infinite recursion. RecursiveProc( int safetyCounter ) If ( safetyCounter > SAFETY_LIMIT ) { return; } safetyCounter = safetyCounter + 1 ... RecursiveProc( safetyCounter ) End Sub

TIPS FOR USING RECURSION Limit recursion to one routine. Keep an eye on the stack. Safety Counter can help limit stack use Also consider heap allocation Don’t use recursion for simple tail recursion (like factorials or Fibonacci)

TIPS FOR USING RECURSION int Factorial( int number ) { if ( number == 1 ) { return 1; } else { return number * Factorial( number - 1 );

TIPS FOR USING RECURSION int Factorial( int number ) { int intermediateResult = 1; for ( int factor = 2; factor <= number; factor++ ) { intermediateResult = intermediateResult * factor; } return intermediateResult;

goto Edsger Dijkstra: Go To Statement Considered Harmful (1968) Very difficult to showcode with goto’s is correct. Whereas code without goto’s can be proven correct!

Goto – hard to read Code containing gotos is hard to format Indentation should be used to show logical structure, and gotos have an effect on logical structure. Using indentation to show the logical structure of a goto and its target, however, is difficult or impossible.

Goto – compiler optimization Use of gotos defeats compiler optimizations. An unconditional goto makes the flow harder to analyze and reduces the ability of the compiler to optimize the code. People THINK gotos are smaller and faster, but the opposite is often true!

Goto – structural nightmare use of gotos leads to the violation of the principle that code should flow strictly from top to bottom.

goto – Spaggetti code Job Security???

goto – support? Is there a place for it?? A well-placed goto can eliminate the need for duplicate code. Duplicate code leads to problems if the two sets of code are modified differently. Duplicate code increases the size of source and executable files.

goto – support? In some cases, the goto can result in faster and smaller code. Good programming doesn't mean eliminating gotos. (just using them when they improve the code) Decades' worth of research with gotos failed to demonstrate their harmfulness. goto has been incorporated into many modern languages (C++)

THE PHONY GOTO DEBATE Arguers' set up straw man arguments: Simple examples to prove a point without really having any generalizable merit Often show code using goto, and then show how to rewrite without.

ERROR PROCESSING AND GOTOS Writing highly interactive code calls for paying a lot of attention to error processing and cleaning up resources when errors occur.

Processing errors goto ' This routine purges a group of files. Sub PurgeFiles( ByRef errorState As Error_Code ) Dim fileIndex As Integer Dim fileToPurge As Data_File Dim fileList As File_List Dim numFilesToPurge As Integer MakePurgeFileList( fileList, numFilesToPurge ) errorState = FileStatus_Success fileIndex = 0 While ( fileIndex < numFilesToPurge ) fileIndex = fileIndex + 1 If Not ( FindFile( fileList( fileIndex ), fileToPurge ) ) Then errorState = FileStatus_FileFindError GoTo END_PROC End If goto

Processing errors goto goto goto Goto target If Not OpenFile( fileToPurge ) Then errorState = FileStatus_FileOpenError GoTo END_PROC End If If Not OverwriteFile( fileToPurge ) Then errorState = FileStatus_FileOverwriteError if Not Erase( fileToPurge ) Then errorState = FileStatus_FileEraseError End While END_PROC: DeletePurgeFileList( fileList, numFilesToPurge ) End Sub goto goto goto Goto target

No goto ' This routine purges a group of files. Sub PurgeFiles( ByRef errorState As Error_Code ) Dim fileIndex As Integer Dim fileToPurge As Data_File Dim fileList As File_List Dim numFilesToPurge As Integer MakePurgeFileList( fileList, numFilesToPurge ) errorState = FileStatus_Success fileIndex = 0 While ( fileIndex < numFilesToPurge And errorState = FileStatus_Success ) fileIndex = fileIndex + 1

No goto If FindFile( fileList( fileIndex ), fileToPurge ) Then If OpenFile( fileToPurge ) Then If OverwriteFile( fileToPurge ) Then If Not Erase( fileToPurge ) Then errorState = FileStatus_FileEraseError End If Else ' couldn't overwrite file errorState = FileStatus_FileOverwriteError Else ' couldn't open file errorState = FileStatus_FileOpenError Else ' couldn't find file errorState = FileStatus_FileFindError <-- 2 End While DeletePurgeFileList( fileList, numFilesToPurge ) End Sub

No goto – status variable ' This routine purges a group of files. Sub PurgeFiles( ByRef errorState As Error_Code ) Dim fileIndex As Integer Dim fileToPurge As Data_File Dim fileList As File_List Dim numFilesToPurge As Integer MakePurgeFileList( fileList, numFilesToPurge ) errorState = FileStatus_Success fileIndex = 0

No goto – status variable While ( fileIndex < numFilesToPurge ) And ( errorState = FileStatus_Success ) fileIndex = fileIndex + 1 If Not FindFile( fileList( fileIndex ), fileToPurge ) Then errorState = FileStatus_FileFindError End If If ( errorState = FileStatus_Success ) Then If Not OpenFile( fileToPurge ) Then errorState = FileStatus_FileOpenError

No goto – status variable If ( errorState = FileStatus_Success ) Then If Not OverwriteFile( fileToPurge ) Then errorState = FileStatus_FileOverwriteError End If If Not Erase( fileToPurge ) Then errorState = FileStatus_FileEraseError End While DeletePurgeFileList( fileList, numFilesToPurge ) End Sub

No goto - try ' This routine purges a group of files. Exceptions are passed to the caller. Sub PurgeFiles() Dim fileIndex As Integer Dim fileToPurge As Data_File Dim fileList As File_List Dim numFilesToPurge As Integer MakePurgeFileList( fileList, numFilesToPurge )

No goto - try Try fileIndex = 0 While ( fileIndex < numFilesToPurge ) fileIndex = fileIndex + 1 FindFile( fileList( fileIndex ), fileToPurge ) OpenFile( fileToPurge ) OverwriteFile( fileToPurge ) Erase( fileToPurge ) End While Finally DeletePurgeFileList( fileList, numFilesToPurge ) End Try End Sub

SUMMARY OF GUIDELINES FOR USING GOTOS Use gotos to emulate structured control constructs in languages that don't support them directly. Don't use the goto when an equivalent built- in construct is available. Measure the performance of any goto used to improve efficiency. Limit yourself to one goto label per routine unless you're emulating structured constructs.

SUMMARY OF GUIDELINES FOR USING GOTOS Limit yourself to gotos that go forward, not backward, unless you're emulating structured constructs. Make sure all goto labels are used. Make sure a goto doesn't create unreachable code. If you're a manager, adopt the perspective that a battle over a single goto isn't worth the loss of the war.