 Thursday: › Team Presentations › Risk Assessment and project plan due 11:55 pm  Friday: › Help on coding/testing  Monday: › HW 5 due, 11:55 pm.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Statement-Level Control Structures
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Software Engineering Table-driven methods General control issues.
ISBN Chapter 8 Statement-Level Control Structures.
Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in Programming Issues with Return statements Recursion and.
Software Engineering Linear code Conditionals. Linear code 1.Statements that must be in a specific order. 2.Statements whose order does not matter.
ISBN Chapter 8 Statement-Level Control Structures.
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Program Design and Development
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
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:
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Unusual Control Structures and General Control Issues A Presentation by CJ Castellani as told in an American accent.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
ISBN Chapter 8 Statement-Level Control Structures.
sequence of execution of high-level statements
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Controlling Execution Dong Shao, Nanjing Unviersity.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Control Structures sequence of execution of high-level statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Statement Level Flow of Control GOTOs and Other Weirdness Copyright © by Curt Hill.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Fundamental Programming Fundamental Programming More on Repetition.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
W E E K F I V E Statement-Level Control Structures.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
CSE3302 Programming Languages (notes continued)
Def: A control structure is a control statement and
16. Controlling Loops CSC-3004 Introduction to Software Development
Chapter 3: Decisions and Loops
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Control Structures II (Repetition)
Chapter 8: Control Structures
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
MSIS 655 Advanced Business Applications Programming
Statement-Level Control Structures
16. Controlling Loops CSC-3004 Introduction to Software Development
17. Unusual Control Structures
Statement-Level Control Structures
Control Structure Testing
CMPE212 – Reminders The other four assignments are now posted.
15. Using Conditionals CSC-3004 Introduction to Software Development
Chapter8: Statement-Level Control Structures April 9, 2019
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Controlling Program Flow
Presentation transcript:

 Thursday: › Team Presentations › Risk Assessment and project plan due 11:55 pm  Friday: › Help on coding/testing  Monday: › HW 5 due, 11:55 pm

 Case Statements  Loops  Unusual control structures  Go-to debate  General control issues

 Nominal path through first › Then write unusual cases  Check branches › > instead of >= is analogous to off-by-one  Put the normal case after the if  Follow the if clause with a meaningful statement

 Consider the else clause › GM analysis: 50-80% of if statements should have an else clause  Test the else clause for correctness  Check for reversal of the if and else clauses

 Simplify complicated test with boolean function calls  Put the most common cases first  Make sure all cases are covered  Replace with other constructs, if supported

 Order cases alphabetically or numerically  Normal case first  Order cases by frequency

 Keep the actions of each case simple  Don’t make up phony variables  Use the default clause only to detect legitimate defaults  Avoid dropping through

 Enter only from one location  Put initialization code directly before the loop  Use while( true ) for infinite loops  Prefer for loops when appropriate

 Use brackets  Avoid empty loops  Keep loop-housekeeping at either the beginning or the end  Make each loop perform only one function

 Assure yourself that the loop ends  Make loop-termination conditions obvious  Don’t mess with the loop index to make the loop terminate  Avoid code that depends on the loop index’s final value  Consider using safety counters

 Consider using break statements over boolean flags  Be wary of loops with lots of breaks › January 15, 1990: NYC’s phone system halted for 9 hrs do to an extra break  Use continue for tests at the top of a loop  Use the labeled break structure if supported  Use break and continue only with caution

 Use ordinal or enumerated types for limits on both arrays and loops  Use meaningful variable names to make nested loops readable  Use meaningful names to avoid loop- index cross-talk  Limit the scope of loop-index variables to the loop itself

 Short enough to view all at once  Limit nesting to three levels  Move loop innards of long loops into routines  Make long loops especially clear

 Use a return when it enhanced readability  Use guard clauses (early returns/exits) to simplify complex error processing  Minimize the number of returns in each routine

 Make sure the recursion stops  Use safety counters to prevent infinite recursion  Limit recursion to one routine › Cyclic: A calls B calls C calls A  Keep an eye on the stack

40 years in the making

 Higher-quality without › “Go To Statement Considered Harmful”, by Edsger Dijkstra, Communications of the ACM, March 1968  Hard to format  Defeats complier optimizations  Not necessarily small or faster › “Structured Programming with go to Statements” by Donald Knuth  Violation of the principle that code should flow from top to bottom  Many modern languages (like Java) don’t even have gotos

 Careful use in specific circumstances rather than indiscriminant use › No loops in Fortran  Can eliminate duplicate code  Useful in a routine that allocates resources, performs operations on those resources, and then deallocates the resources  Smaller and faster › See again Knuth’s article  Incorporated into many modern languages › Including Ada

 Compare boolean values to true and false implicity  Break complicated tests into partial test with new boolean variables  Move complicated expressions into boolean functions  Use decision tables to replace complicated conditions

Initial expressionEquivalent Expression not A and not Bnot (A or B) not A and Bnot (A or not B) A and not Bnot (not A or B) A and Bnot (not A or not B) not A or not Bnot (A and B) not A or Bnot (A and not B) A or not Bnot (not A and B) A or Bnot (not A and not B) DeMorgan’s Theorem