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

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Programming Languages and Paradigms
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Recursion. Binary search example postponed to end of lecture.
ISBN Chapter 10 Implementing Subprograms.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Prof. S.M. Lee Department of Computer Science. Answer:
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Programming Epson Robots – Part 2 ME 4135 – Fall 2012 Dr. R. Lindeke.
UNIT II Decision Making And Branching Decision Making And Looping
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
Language Evaluation Criteria
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
ISBN Chapter 10 Implementing Subprograms.
GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,
 Thursday: › Team Presentations › Risk Assessment and project plan due 11:55 pm  Friday: › Help on coding/testing  Monday: › HW 5 due, 11:55 pm.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Sequential & Object oriented Programming
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 Statements: Control Structure Issues (Chapters and 19 of Code Complete) Don Bagert CSSE 375, Rose-Hulman October 17, 2006.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
FORTRAN History. FORTRAN - Interesting Facts n FORTRAN is the oldest Language actively in use today. n FORTRAN is still used for new software development.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Programming Techniques
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
CSE3302 Programming Languages (notes continued)
Def: A control structure is a control statement and
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Topics Introduction to Repetition Structures
Chapter 5 Conclusion CIS 61.
Introduction to C++ Recursion
Topics Introduction to File Input and Output
Chapter 4 void Functions
17. Unusual Control Structures
Three Special Structures – Case, Do While, and Do Until
Basics of Recursion Programming with Recursion
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Yan Shi CS/SE 2630 Lecture Notes
Decisions, decisions, decisions
Introduction to Programming
Topics Introduction to File Input and Output
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Statements and Control 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 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

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

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

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

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

The End goto end_of_presentation